#include<iostream>
using std::cout;
using std::endl;
class C
{
public:
C(int x)
{
this->x=x;
}
int getX() const
{
//this->x=12;//error C2166: 左值指定 const 对象
return this->x;
}
void setX(int x)
{
this->x=x;
}
private:
int x;
};
int main()
{
const C c(13);
c.setX(10);// error C2662: “C::setX”: 不能将“this”指针从“const C”转换为“C &”
cout<<c.getX()<<endl;
return 0;
}
重学C++ const对象
最新推荐文章于 2025-02-11 20:56:12 发布