#include<iostream>
using namespace std;
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");
}
#include<iostream>
using namespace std;
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 使其变为常变量,只有const 函数才能调用它,这比private 更具有隐蔽性 更安全。。