When I'm in Sub::f()
and try to access the protected members of another sub-class to which the Base* b
pointer points to, it won't compile until Sub
is a friend of Base. Why do I need to do this?
class Base{
//friend class Sub;
protected:
int i;
virtual void f() = 0;
};
class Sub : public Base{
Base* b;
public:
Sub(Base* ba) : b(ba){}
void f(){
b->f();
cout << b->i << endl;
}
};