重学C++ const成员函数

来源:互联网 发布:推饼游戏源码 编辑:程序博客网 时间:2024/06/04 17:49
#include<iostream>using std::cout;using std::endl;class C{public:int getX() const{this->x=12;//error C2166: 左值指定 const 对象return this->x;}void setX(int x){this->x=x;}private:int x;};int main(){C c;c.setX(10);cout<<c.getX()<<endl;return 0;}