File: pointerconv.yo

package info (click to toggle)
c%2B%2B-annotations 10.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,536 kB
  • ctags: 3,247
  • sloc: cpp: 19,157; makefile: 1,521; ansic: 165; sh: 128; perl: 90
file content (62 lines) | stat: -rw-r--r-- 2,714 bytes parent folder | download
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
52
53
54
55
56
57
58
59
60
61
62
    We return to our tt(Vehicle) classes, and define the following objects and
pointer variable:
        verb(
    Land land(1200, 130);
    Car auto(500, 75, "Daf");
    Truck truck(2600, 120, "Mercedes", 6000);
    Vehicle *vp;
    )
    Now we can assign the  addresses of the three objects of
the derived classes to the tt(Vehicle) pointer:
        verb(
    vp = &land;
    vp = &auto;
    vp = &truck;
        )
    Each of these assignments is acceptable. However, an
    i(implicit conversion) of the i(derived class) to the i(base class)
tt(Vehicle) is used, since tt(vp) is defined as a pointer to a
tt(Vehicle). Hence, when using tt(vp) only the member functions manipulating
tt(mass) can be called as this is the tt(Vehicle)'s em(only) functionality.
As far as the compiler can tell this is the object tt(vp) points to.

    The same holds true for hi(reference) references to
tt(Vehicles). If, e.g., a function is defined having a tt(Vehicle) reference
parameter, the function may be passed an object of a class derived from
tt(Vehicle). Inside the function, the specific tt(Vehicle) members remain
accessible. This analogy between pointers and references holds true in
general. Remember that a reference is nothing but a i(pointer in disguise): it
mimics a plain variable, but actually it is a pointer.

    This restricted functionality has an important consequence
for the class tt(Truck). Following  tt(vp = &truck), tt(vp) points to
a tt(Truck) object. So, tt(vp->mass()) returns 2600 instead of
8600 (the combined mass of the cabin and of the trailer: 2600 + 6000),
which would have been returned by tt(truck.mass()).

    When a function is called using a i(pointer to an object), then the
    emi(type of the pointer) (and not the type of the object) determines
which
    hi(available member functions) hi(member function: available) member
functions are available and can be executed.  In other words, bf(C++)
implicitly converts the type of an object reached through a pointer to the
pointer's type.

    If the actual type of the object pointed to by a pointer is known, an
explicit type cast can be used to access the full set of member functions
that are available for the object: hi(static_cast)
        verb(
    Truck truck;
    Vehicle *vp;

    vp = &truck;        // vp now points to a truck object

    Truck *trp;

    trp = static_cast<Truck *>(vp);
    cout << "Make: " << trp->name() << '\n';
        )
    Here, the second to last statement specifically casts a tt(Vehicle *)
variable to a tt(Truck *). As usual (when using casts), this code
is not without risk. It em(only) works  if tt(vp) really points to a
tt(Truck). Otherwise the program may produce unexpected results.