
#include
using namespace std;
class studentInfo
{
public:
void setScore(int score){this->score=score;}
int getScore() const{printScore();return score;}
void printScore(){cout<
int score;
};
int main(void)
{
return -1;
}
如果非要在const成员函数里面修改成员变量的话该怎么做了?上网查了一下主要有一下两种方法:
1. 通过this指针进行类型强制转换实现
类似于下面这样的方法:
int getScore() const
{
(const_cast
return score;
}
2. 将成员变量定义为mutable
类似于下面这样:
mutable int score;
下一篇:explicit怎么构造函数