4 Up cast issues

Member hiding (as a result of private or protected inheritance) can lead to type casting problems. This is because up casting a subclass that has inheritance limiters can reexpose members that are less restricted in the superclass.

Listing 5 illustrate this kind of problem.


Listing 5:upcastproblem
 
1class A 
2{ 
3  public
4    int i; 
5}
6 
7class B : public A 
8{ 
9  // B-specific members 
10}
11 
12class C : protected A 
13{ 
14  // C-specific members 
15}
16 
17void test(void
18{ 
19  A *pA; 
20  B myB; 
21  C myC; 
22 
23  pB = &myB; // no problem 
24  pC = &myC; // error! 
25}

The error reported by g++ is “error: ’A’ is an inaccessible base of ’C’ ”.