3 Ambiguous cases

Listing 2 illustrates an ambiguous case, where two superclasses use the same name.


Listing 2:ambiguous
 
1class A 
2{ 
3  public
4    int i; 
5    int j; 
6}
7 
8class B 
9{ 
10  public
11    int j; 
12}
13 
14class C : public A, public B 
15{ 
16  public
17    int k; 
18}
19 
20void test(void
21{ 
22  C myC; 
23 
24  myC.i = 0; 
25  myC.j = 0;  
26  myC.k = 0; 
27}

In this case, the compiler catches the problem, and generates errors as follows:

ambiguous.cc: In function ’void test()’:  
ambiguous.cc:25: error: request for member ’j’ is ambiguous  
ambiguous.cc:11: error: candidates are: int B::j  
ambiguous.cc:5: error:                 int A::j

The error message is quite clear, and it points to the possible candidates. Note that the error is on line 25, and not in the definition of class C. In other words, class C does inherit both properties named j from A and B.

To fix this problem, replace line 25 with

myC.B::j = 0;

In other words, instead of saying “property j of myC”, we now say “property j inherited from class B of myC”.