错误代码
class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX(){return x;}
};
void main()
{
const C c(5);
cout<<c.getX();
system("pause");
}
改法一
class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX()const{return x;}
};
void main()
{
const C c(5);
cout<<c.getX();
system("pause");
}
改法二
class C
{
private:
int x;
public:
C(int x)
{
this->x=x;
}
int getX()
{
return x;
}
};
void main()
{
C c(5);
cout<<c.getX();
system("pause");
错误出现在const常变量这里,改法二在主函数调用时或许更方便些