class B :public A
{
// B's own defns
};
In this case, we mean the following:
public in A is visible and public in B
protected in A is visible and protected in B
private in A is not visible in B
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:
public in A is visible and protected in B
protected in A is visible and protected in B
private in A is not visible in B
Lastly, we have the following
class B : A
{
// B's own defns
};
The absense of a scope restricter means it is private:
public in A is visible and protected in B
protected in A is visible and protected in B
private in A is not visible in B
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