开发者

Value access in C++

HI,

I have two clases A and B, Here A is inheriting B and now i want to access a variable in B from A, I included A header in B and tried to access but showing some error in QObject.

Is it possible to开发者_JS百科 acces like this.. Please help


Not sure i get your Q correctly....

class A {
public:
  int nValueA;
protected:
  int nValueB;
private:
  int nValueC;
};

class B : public A {
public:
    B();
    int x, y, z;
}; 
B::B(): 
x(nValueA), //-->OK
y(nValueB), //-->OK
z(nValueC)  //-->error due to child can't inherit parent's private member
{}

void main(){
  B object;
  object.nValueA = 888; //--> valid
  object.nValueB = 888; //--> error since protected member is not accessible
  object.nValueC = 888; //--> error since private member is not accessible
}

Possible solution:

class A {
public:
  int nValueA;
  int nValueB;
  int nValueC;
};


Is your member variable private? Then you cannot, declare it protected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜