I don't understand the following, when Derived inherits from Base, it gets access to its protected members which can be accessed through Derived functions. But if, Base class tries to access its own members from Derived class (which itself allows access to Base), it doesn't get access, why?
class Base {
protected:
int x;
};
class Derived : Base {
public:
void foo(Base* b);
};
void Derived::foo(Base* b) {
b->x = 2; // cannot access protected member,
// though Derived inherits from Base, why?
}
Derivedisn't accessing it's own members derived fromBase. It's accessing some other objects member's derived fromBase.