源程序:
#include <iostream>
using namespace std;
class ctest
{
private:
int x;
public:
ctest(const int x)
{
this->x = x;
}
int getx() const //const必须写在函数的后面
{
return x;
}
};
int main()
{
const ctest obj(5); //常对象
cout << obj.getx() << endl; //常对象调用常成员函数
system("pause");
return 0;
}
运行结果: