c++一道查错题

来源:百度知道 编辑:UC知道 时间:2024/06/04 19:29:49
3. 查错题

class A
{
A(){mpArray = 0;}
~A(){}
public:
int &getCount(void) {int m = mValue; return m;}
static initCount(int iCount) {mValue = iCount;}
void mallocArray( int iLen) { mpArray = new int [iLen];}
void freeArray(void) {delete mpArray;}
protected:
int mValue;
int *mpArray;

};

答案:

静态函数不能访问类中的非静态成员,而且你的静态函数还没有返回值

static initCount(int iCount) {mValue = iCount;} 这里错了,将static改成void吧

static initCount(int iCount) 里
你的mValue是protected,而函数是static;
static成员函数无法访问protected的成员