I have a simple class as below
class A {
protected:
int x;
};
class B:public A
{
public:
int y;
void s
Note that B does not have FULL access to A::x. It can only access that member through an instance of a B, not anything of type A or deriving from A.
There is a workaround you can put in:
class A
{
protected:
int x;
static int& getX( A& a )
{
return a.x;
}
static int getX( A const& a )
{
return a.x;
}
};
and now using getX, a class derived from A (like B) can get to the x member of ANY A-class.
You also know that friendship is not transitive or inherited. The same "workaround" can be made for these situations by providing access functions.
And in your case you can actually provide "public" access to the x through your B by having public functions that get to it. Of course in real programming it's protected for a reason and you don't want to give everything full access, but you can.