1.对于const修饰的成员函数分为两种观点: logic constness and bitwise constness.
bitwise constness 的观点就是说 :“它不更改对象的任一bit”, 这样一来 , 编译器只要成员变量有赋值的行为即可,这是对只读性的定义,因此,const 成员函数不可以更改对象中任一non- static 成员变量。
eg1:
class TextBlock
{
public:
............
char& operator [] (std::size_t position) ;
const char& operator [] (std::size_t position) const ;
private:
std::string text ;
} ;
TextBlock bt("Jack") ;
std::cout< bt[0] = 'T' ; //true , write a non-const TextBlock const TextBlock cbt("Rose") ; std::cout< cbt[0] = 'L' ; //false , cannot write a const TextBlock !! char *ptr = &ctb[0] ; *ptr = 'T' ; //true , not change the adress of ctb[0] , but the content has changed!! 而logical constness的观点是可以改变对象内的某些bits ,例如eg1中的*ptr = 'T' ; 这样还是让ctb中的pText 值改变了!! 若类的常成员函数有non-const 数据成员,并且要进行赋值操作, 那只能用mutable 来修饰non-const 数据成员了 eg2: class TextBlock { public: TextBlock(char * str = "\0" ) : pText(str) , textLength(0) , lengthIsValid(false) {} std::size_t length() const ; private: std::size_t textLength ; bool lengthIsValid ; } ; std::size_t TextBlock::length() const { if(!lengthIsValid) { textLength = std::strlen(pText) ; lengthIsValid = true ; } return textLength ; } 2.在const 与non-const 成员函数避免重复,这时候需要进行解除const的只读性和加上const 的只读性。 eg3: class TextBlock { public: TextBlock(string name = "\0") ; const char& operator [] (std::size_t position ) const ; //常成员函数 char& operator [](std::size_t position) ; //普通成员函数 private: std::string m_Name ; } ; TextBlock::TextBlock(string name):m_Name(name) {} const char& TextBlock::operator [] (std::size_t position) const { if(position >= m_Name.length() || position < 0) //边界检查 { throw 0 ; //抛出异常 } else { return m_Name[position] ; } } //利用普通成员函数去调用常成员函数 char& TextBlock::operator[] (std::size_t position) { //static_cast //cosnt_cast是把常operator[]的返回常量去掉 //若不这样进行两次转换,会出现无限的递归调用non-const operator[] 函数 return const_cast } www.2cto.com