views:

112

answers:

2

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 acces like this.. Please help

+1  A: 

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

Benoit
+3  A: 

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;
};
wengseng