File: ambiguity.yo

package info (click to toggle)
c%2B%2B-annotations 8.2.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,804 kB
  • ctags: 2,845
  • sloc: cpp: 15,418; makefile: 2,473; ansic: 165; perl: 90; sh: 29
file content (51 lines) | stat: -rw-r--r-- 1,942 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Let's investigate closer why an tt(AirAuto) introduces i(ambiguity), when
derived from tt(Auto) and tt(Air).
    itemization(
    it() An tt(AirAuto) is an tt(Auto), hence a tt(Land), and hence a
tt(Vehicle).
    it() However, an tt(AirAuto) is also an tt(Air), and hence a tt(Vehicle).
    )
    The duplication of tt(Vehicle) data is further illustrated in
fig(ambiguity).
    figure(polymorphism/ambiguity)
        (Duplication of a base class in multiple derivation.)
        (ambiguity)
    The internal organization of an tt(AirAuto) is shown in
fig(InternalOrganization)
    figure(polymorphism/internal)
        (Internal organization of an tt(AirAuto) object.)
        (InternalOrganization)
    The bf(C++) compiler detects the ambiguity in an tt(AirAuto) object,
and will therefore not  compile  statements like:
        verb(
    AirAuto jBond;
    cout << jBond.mass() << '\n';
        )
    Which member function tt(mass) to call cannot be determined by the
compiler but the programmer has two possibilities to resolve the ambiguity for
the compiler:
    itemization(
    it() First, the function call where the ambiguity originates can be
modified. The ambiguity is resolved using the i(scope resolution operator):
        verb(
    // let's hope that the mass is kept in the Auto
    // part of the object..
    cout << jBond.Auto::mass() << '\n';
        )
    The scope resolution operator and the class name are put right before
the name of the member function.
    it() Second, a dedicated function tt(mass) could be created for
the class tt(AirAuto):
        verb(
    int AirAuto::mass() const
    {
        return Auto::mass();
    }
        )
    )
    The second possibility is preferred as it does not require the compiler to
flag an error; nor does it require the programmer using the class
tt(AirAuto) to take special precautions.

    However, there exists a more elegant solution, discussed in the next
section.