(c++常问问题六)类的成员函数后面加const有什么用

每个类的成员函数都默认传入this指针,成员函数后面加了const后该成员函数将不能修改该类的成员了

 

class cat
{
public:
  cat(){};
  
  string getName() const
  {
      this->m_strName = “”;//错误,const this不允许修改成员
      return this->m_strName; //正确,没修改
   }

protected:
  string m_strName;
}

 

posted @ 2016-01-28 23:55  Jensen抹茶喵  阅读(433)  评论(0编辑  收藏  举报