Consider following code:
namespace base {
class Base {
protected:
class Nested {
protected:
Base* base;
public:
Nested(Base* _base) : base( _base ){}
virtual void test() {
base->foo();
/*
* hmm.. we can use protected methods
*/
base->bar();
}
};
protected:
Nested* nested;
void bar(){}
public:
void foo(){}
virtual void test() {
nested = new Nested(this);
nested->test();
}
};
};
namespace inherited {
class Base : public base::Base {
public:
Base()
: base::Base() {}
protected:
class Nested : public base::Base::Nested {
public:
Nested( inherited::Base* base )
: base::Base::Nested( base ) {}
public:
virtual void test() {
base->foo();
/*
* hmm.. and now they are not accessible
*/
// base->bar();
}
};
public:
virtual void test() {
foo();
bar();
nested = new Nested(this);
nested->test();
}
};
};
My qestions is why we have access to protected methods/properties of base::Base from base::Base::Nested but no access to the same methods/properties of inherited::Base from inherited::Base::Nested?
The only thing I could suppose is that base::Base is a kind of global scope for base::Base::Nested thus they are accessible. inherited::Base is a kind of global scope for inherited::Base::Nested and protected members of base::Base are not accessible. However, public inheritance should not change scope of visibility and the reason of access inability is unclear for me.
The issue seems to be the type of the stored pointer and not access rights. Consider this:
The situation is similar when you try to call
barthrough thepointer that is stored in the base.
You can work around this issue by down-casting base, but I would strongly object to that.