您的位置:首页 > 编程语言 > C语言/C++

回顾经典Effective C++ 3 4

2014-07-12 01:20 155 查看
Base of "const":

强调常量性,不可修改性。

eg:

char greeting[] = "hello";
char *p = greeting;
const char*q = greeting;
char* const q = greeting;
const char*const q = greeting;


Extend Of "const":

const 成员函数:

{

bitwise constness;编译器理念的不可修改性

logical constness;^up

}

1.
class Ctest
{
public:
char& operator[](std::size_t pos)const
{
return ptext[pos];
}
private:
char* ptext;
}
如上返回的引用可修改内部ptext,这点个人认为不好。

2.对应的logical constness可修改const函数内的赋值行为:

mutable

notice-3:

当const和non-const成员函数有着实质等价,可令non-const版本调用const版本避免代码重复。

notice-4:

为免除"跨编译单元之初始化次序"问题,请以local static 对象替换non-local static 对象。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: