5 Public, protected and private inheritance

Observe the following code:

class B :public A
{
  // B's own defns
};

In this case, we mean the following:

This means that class B is not attempting to hide anyting that is inherited from class A. This is the only time when up casting is permitted with static_cast and dynamic_cast.

We can change the code just a little bit:

class B :protected A
{
  // B's own defns
};

This means the following:

Lastly, we have the following

class B : A
{
  // B's own defns
};

The absense of a scope restricter means it is private:

In the cases of protected and private inheritance, up casting using static_cast or dynamic_cast generates compile-time errors. This is because an up cast can reexpose information that should be hidden.

Of course, we can get around the compiler using reinterpret_cast (or C style cast). However, that is dangerous, and it can easily lead to problems.

Copyright © 2006-10-12 by Tak Auyeung