问一个关于指针在类中初始化的问题!
定义了一个类:class SimpleCat
{
public:
SimpleCat();
~SimpleCat();
void SetAge(int age){*itsAge=age;}
int GetAge(){return *itsAge;}
void SetWeight(int weight){*itsWeight=weight;}
int GetWeight(){return *itsWeight;}
private:
int *itsAge=0;
int *itsWeight;
};
SimpleCat::SimpleCat()
{
itsAge=new int(2);
itsWeight=new int(20);
}
SimpleCat::~SimpleCat()
{
delete itsAge;
delete itsWeight;
}
红色区域的私有数据用指针表示,但是为什么我给指针赋初值,编译器就报错啊?书上不说最好每次声明指针的时候都赋初值吗?
错误:error C2252: 'itsAge' : pure specifier can only be specified for functions