成员函数:
class myStr
{
public:
bool equal(const myStr& strB){ return strName==strB.strName}
private:
std::string strName;
}
假如有两个对象strA和strB,当strA.equal(strB)时,实际上将this指针(即调用对象strA的地址)隐式地传入了myStr类的成员函数里,那么equal函数运行时即是:return this.strName==strB.strName,也即是strA.strName==strB.strname。
而常量成员函数:
bool equal(const myStr& strB) const {return strName==strB.strName}
由于this 是指向调用对象的指针,const成员函数传递的是const this对象指针,所以const成员函数不能修改调用该函数的对象,因此equal函数只能读取而不能修改调用它们的对象的数据成员。