每个类的成员函数都默认传入this指针,成员函数后面加了const后该成员函数将不能修改该类的成员了
class cat
{
public:
cat(){};
string getName() const
{
this->m_strName = “”;//错误,const this不允许修改成员
return this->m_strName; //正确,没修改
}
protected:
string m_strName;
}
每个类的成员函数都默认传入this指针,成员函数后面加了const后该成员函数将不能修改该类的成员了
class cat
{
public:
cat(){};
string getName() const
{
this->m_strName = “”;//错误,const this不允许修改成员
return this->m_strName; //正确,没修改
}
protected:
string m_strName;
}
转载于:https://www.cnblogs.com/JensenCat/p/5167805.html