(c++常问问题六)类的成员函数后面加const有什么用
每个类的成员函数都默认传入this指针,成员函数后面加了const后该成员函数将不能修改该类的成员了
class cat
{
public:
cat(){};
string getName() const
{
this->m_strName = “”;//错误,const this不允许修改成员
return this->m_strName; //正确,没修改
}
protected:
string m_strName;
}

浙公网安备 33010602011771号