#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");
}
#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");
}
第二种好,第二种在运算的过程中,不会改变值。使结果不会发生改变。
仔细看const在什么地方,在判断。