#define ASPECT_RATIO 1.653
ASPECT_RATIO 未进入记号表,因此调试时,错误信息只会提到 1.653 而不是 ASPECT_RATIO。
因此,替换为:
const double AspectRatio = 1.653;
改用常量 AspectRatio 还可能比宏 ASPECT_RATIO 用较小的码,预处理器盲目将宏替换成值,常量则只会有一份值存在。
const char* const authorName = "tofu";
定义常量指针,必须写两次 const。
因此,替换成:
const std::string authorName("tofu");
这样更方便。
// .h
class GamePlayer{
private:
static const int NumTurns = 5; // 常量声明式
int scores[NumTurns];
}
如果需要取某个 class 专属常量的地址,你必须提供如下的定义式:
// .cpp const int GamePlayer::NumTurns; // 常量定义式
如果编译器不支持上述写法,你可以将初值放在定义式:
// .h
class CostEstimate{
private:
static const double FudgeFactor; // 常量声明式
}
// .cpp const double CostEstimate::FudgeFactor = 1.35; // 常量定义式
由于编译器在编译时必须知道数组的大小,因此如果编译器(错误地)不允许“static const int IN class”完成“IN class”的初值定义,则可用如下写法:
class GamePlayer{
private:
enum{ NumTurn = 5 }; // "the enum hack"
int scores[NumTurns];
}
enum hack 方案与 #define 有些相似,例如取一个 const 的地址是合法的,但取一个 enum 和取 #define 的地址是不合法的。
改用 inline 内联函数替换 #define 形似函数。
const int * p; // const 在 * 前,指针->常量 int const * p; // 和上面一个意思 int * const p; // const 在 * 后,常量指针->变量 const int * const p; // const 在 * 前后都有,常量指针->常量 // STL 迭代器,特殊情况 std::vectorvec; const std::vector ::iterator iter = vec.begin(); // 类似 T * const std::vector ::const_iterator cIter = vec.begin(); // 类似 const T *
看关键字 const 时,看它在星号前后即可。在哪边就修饰哪一边(type / var)。
const Rational operator*(const Rational& lhs, const Rational& rhs);
为什么返回值是一个 const 对象呢?其实是为了防止这种行为:
Rational a, b, c; (a * b) = c; // 在 a * b 的结果上调用 operator= if(a * b = c) // eg. 其实是想做一个比较动作!
一个 const 也许就能避免“==”意外键成“=”的错误,何乐不为呢、
const 成员函数,如果两个成员函数只是常量性不同,它是可以被重载的。
class TextBlock{
public:
const char& operator[](std::size_t position) const{ // operator[] for const 对象
return text[position];
}
char& operator[](std::size_t position){ // operator[] for non-const 对象
return text[position];
}
private:
std::string text;
}
TextBlock 的 operator[] 可以被这么用:
TextBlock tb("Hello");
std::cout << tb[0]; // 调用 non-const TextBlock::operator[]
const TextBlock ctb("World");
std::cout << ctb[0]; // 调用 const TextBlock::operator[]
需要注意的是,non-const operator[] 的返回类型是个 reference to char(char&),不是 char。如果返回的是一个 char,那么 tb[0] = 'x'; 将无法通过编译。
、运用 const 成员函数实现其 non-const 成员函数:(减少代码重复量)
class TextBlock{
public:
...
const char& operator[](std::size_t position) const{
... // 边界检验
... // 志记数据访问
... // 检验数据完整性
return text[posittion];
}
char& operator[](std::size_t position){
return
const_cast( // 将 op[] 返回值的const转除
static_cast(*this) // 为 *this 加上 const
[position] // 调用 const op[]
);
}
private:
std::string text;
}
对任何对象进行初始化!
class A{
public:
// 构造函数
A(const std::string name, const int age);
private:
std::string theName;
int theAge;
}
以下行为为赋值,而非初始化:
A::A(const std::string name, const int age){
theName = name;
theAge = age;
}
建议替换为:
A::A(const std::string name, const int age)
:theName(name),
theAge(age) {
// ...
}
解决对象初始化次序问题:
class FileSystem{...}; // 来自你的程序库
FileSystem& tfs(){
static FileSystem fs;
return fs;
}
class Directory{...}; // 由程序库客户建立
Directory::Directory(prams){
...
std::size_t disks = tfs().numDisks(); // 使用 tfs 对象,这样子确保对象初始化
...
}
Directory& tempDir(){
static Directory td;
return td;
}



