R
Rick
Hi,
Can anyone explain to me why the below fails to compile - seeing
otherA->f(); as a call to a inaccessible function, while otherB->f();
is ok?
It seems you can happily access protected functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.
I wanted to implement a recursion down a tree (via parent/child base
class pointers) and keep the virtual functions protected - but this
behaviour seems to force me to make them public.
Any info would be very helpful.
Thanks
Rick
class A
{
protected:
virtual void f() const {};
};
class B : public A
{
protected:
virtual void f() const
{
A::f(); // OK
// OK
B * otherB = new B();
otherB->f();
// Not OK - f() protected....
A * otherA = new B();
otherA->f(); };
};
Can anyone explain to me why the below fails to compile - seeing
otherA->f(); as a call to a inaccessible function, while otherB->f();
is ok?
It seems you can happily access protected functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.
I wanted to implement a recursion down a tree (via parent/child base
class pointers) and keep the virtual functions protected - but this
behaviour seems to force me to make them public.
Any info would be very helpful.
Thanks
Rick
class A
{
protected:
virtual void f() const {};
};
class B : public A
{
protected:
virtual void f() const
{
A::f(); // OK
// OK
B * otherB = new B();
otherB->f();
// Not OK - f() protected....
A * otherA = new B();
otherA->f(); };
};