源地址:http://bbs.csdn.net/topics/300206022
const 成员函数:QVariant Cell::evalExpression(const QString &str, int &pos) const;
==> augment : QVariant Cell::evalExpression(const QString &str, int &pos, const Cell * const this);
正常成员函数:QVariant Cell::evalExpression(const QString &str, int &pos) ;
==> augment : QVariant Cell::evalExpression(const QString &str, int &pos, Cell * const this);
Cell *const this: 意味着this是一个常量指针,始终指向这个对象(但这个对象可以改变,只是this指针不能改为指向其他对象);
const Cell *const this: 除了含有上面的意思外,还说明了,this这个指针所指向的对象也是不可变的。
//c++ primer(中文第五版)中p56。
即成员函数后面加上const限定符后,表达了这个成员函数不会改变当前对象中的数据。
用途例子:重载输出运算符
class String // 仅仅是一个类的例子
{
public:
char display() const; // 此const 成员函数用于输出一个字符
.....
private:
......
friend ostream & operator<<(ostream &os, const String &str); // 定义了一个友元函数以使重载后的输出运算符可以访问String中的内容
};
ostream &operator<<(ostream &os, const String &str)
{
os << str.display();//于此调用了display 函数,且实参str中含有const,为了让编译器可以确定str引用的对象不会被修改,
//所以必须让display函数不能修改当前对象,故display函数必须为const 成员函数,否则报错。
}