class Base
{
public:
void foo() {} // Dummy method to clarify the example
};
class PublicChild : public Base
{
public:
void test()
{
foo(); // OK, we have access to Base public members
static_cast<Base*>(this)->foo(); // OK
}
friend class PublicFriend;
};
class PublicFriend
{
void test(PublicChild* p)
{
p->foo(); // OK, the method is public anyway
static_cast<Base*>(p)->foo(); // OK
}
};
class ProtectedChild : protected Base
{
public:
void test()
{
foo(); // OK, we have access to Base public members
static_cast<Base*>(this)->foo(); // OK
}
friend class ProtectedFriend;
};
class ProtectedFriend
{
void test(ProtectedChild* p)
{
p->foo(); // OK, because we are a friend of ProtectedChild, we have the same visibility as ProtectedChild itself
static_cast<Base*>(p)->foo(); // OK
}
};
class PrivateChild : private Base
{
public:
void test()
{
foo(); // OK, we have access to Base public members
static_cast<Base*>(this)->foo(); // OK
}
friend class PrivateFriend;
};
class PrivateFriend
{
void test(PrivateChild* p)
{
p->foo(); // OK, because we are a friend of PrivateChild, we have the same visibility as PrivateChild itself
static_cast<Base*>(p)->foo(); // OK
}
};
int main()
{
Base b;
b.foo(); // OK: public method
PublicChild p1;
p1.foo(); // OK: public inheritance makes Base::foo public
static_cast<Base>(p1).foo(); // OK
ProtectedChild p2;
// p2.foo(); // error: protected inheritance makes Base::foo protected
// static_cast<Base>(p2).foo(); // error
PrivateChild p3;
// p3.foo(); // error: private inheritance makes Base::foo private
// static_cast<Base>(p3).foo(); // error
return 0;
}