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");
}
1.“cout<<c.getx()"出错,因为getx()函数并非const函数,无法对长成员对象进行操作。
2,可在"int getx()后加上const,使之成为长成员函数,或将”const C c(5)"中 的const去掉,
感悟:只看课本,如同是纸上谈兵,最好 的 办法是多做几道题,贴近实战。