dynamic_cast, static_cast

Type Casting
http://www.cplusplus.com/doc/tutorial/typecasting/

I have been studying casting operators:
dynamic_cast, reinterpret_cast, static_cast and const_cast.

I've tried to understand what for these are.
Mostly the dynamic_cast is/was a hard topic for me.



I've found a clear example here:

Why can't a derived class pointer point to a base class object without casting?
http://stackoverflow.com/questions/9752255/why-cant-a-derived-class-pointer-point-to-a-base-class-object-without-casting?rq=1



I think now I fully understand these casting operators (or maybe).

I have written a code just for testing purposes of dynamic_cast and static_cast.

I put it here, please check it
whether I really understood the topic or not?

Thanks.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
using namespace std;

class Pet {
    public:
        int pa,pb;
        void set_petvalue (int a, int b) { pa=a;pb=b; }
};

// The class Dog inherits the members of the class Pet.
class Dog : public Pet {
    public:
        int dx,dy;
        void set_dogvalue (int x, int y) { dx=x;dy=y; }
};

int main ()
{
//1.)
    cout<< "Inheritance and pointer type-castings: \n\n\n";
    cout<< "class Pet members: / pa, pb, set_petvalue() /\n\n";
    cout<< "Inheritance ---> class Dog : public Pet \n\n";
    cout<< "class Dog members: / pa, pb, set_petvalue(); dx, dy, set_dogvalue() /\n\n\n";

    // Create a new object of class Pet and
    // give the reference of this new object
    // to the pet_pointer:
    Pet * pet_pointer = new Pet;
    cout<< "Pet * pet_pointer = new Pet;\n\n";

    // We can access to the members of class Pet
    // with this pet_pointer of Pet* type:
    int i=10, j=20;
    cout<< "Members of class Pet: \n\n";
    pet_pointer->set_petvalue(i,j);
    cout<< "pet_pointer->set_petvalue("<<i<<","<<j<<")\n\n";
    cout<< "pet_pointer->pa = "<< pet_pointer->pa <<"\n\n";
    cout<< "pet_pointer->pb = "<< pet_pointer->pb <<"\n\n\n";

//2.)
    // Create a new object of class Dog and
    // give the reference of this new object
    // to the dog_pointer:
    Dog * dog_pointer = new Dog;
    cout<< "Dog * dog_pointer = new Dog;\n\n";

    // We can now access to the Dog specific members of class Dog
    // and also to  the common members of Pet and Dog  of class Dog
    // with this dog_pointer of Dog* type:
    int k=100, l=200;
    cout<< "The  common members of Pet and Dog  of class Dog: \n\n";
    dog_pointer->set_petvalue(k,l);
    cout<< "dog_pointer->set_petvalue("<<k<<","<<l<<")\n\n";
    cout<< "dog_pointer->pa = "<< dog_pointer->pa <<"\n\n";
    cout<< "dog_pointer->pb = "<< dog_pointer->pb <<"\n\n\n";
    int m=1000, n=2000;
    cout<< "The  Dog specific members  of class Dog: \n\n";
    dog_pointer->set_dogvalue(m,n);
    cout<< "dog_pointer->set_dogvalue("<<m<<","<<n<<")\n\n";
    cout<< "dog_pointer->dx = "<< dog_pointer->dx <<"\n\n";
    cout<< "dog_pointer->dy = "<< dog_pointer->dy <<"\n\n\n\n";


//3.)
    cout<< "class Pet members: / pa, pb, set_petvalue() /\n\n";
    cout<< "Inheritance ---> class Dog : public Pet \n\n";
    cout<< "class Dog members: / pa, pb, set_petvalue(); dx, dy, set_dogvalue() /\n\n\n";

    cout<< "Okay. Now let's do the pointer type-castings:" <<"\n\n\n";

    cout<< "With the p pointer of Pet* type below \nyou can access to members of Pet (if they exists). \n\n\
Dog has Pet parts from inheritance, \nall of these Pet parts are accessible with p (because p is of Pet* type). \n\n\
We have a full object from the point of view of p:" <<"\n\n\n";
    Pet* p = new Dog;

    cout<< "// ALLOWED: \n\n";
    cout<< "Pet* p = new Dog; " <<"\n\n";

    int r=111, t=222;
    p->set_petvalue(r,t);
    cout<< "p->set_petvalue("<<r<<","<<t<<");\n\n";
    cout<< "p->pa = " << p->pa <<"\n\n";
    cout<< "p->pb = " << p->pb <<"\n\n\n\n";

 /*   cout<< "NOT Accessible: \n\n";  // not allowed by compiler
    r=333, t=444;
    p->set_dogvalue(r,t);
    cout<< "p->set_dogvalue("<<r<<","<<t<<");\n\n";
    cout<< "p->dx = " << p->dx <<"\n\n";
    cout<< "p->dy = " << p->dy <<"\n\n";
*/


//4.)
    cout<< "With the d pointer of Dog* type below \nyou can access to members of Dog (if they exists).\n\
(Remember that Dog has both (common) Pet parts and (specific) Dog parts.)\n\
BUT the new Pet object only have Pet parts and no Dog parts. \n\nWe have an incomplete object from the point of view of pointer d.\n\
(p would have to have access to all the members of Dog.) \n\nYou can access the Pet members of this incomplete object \n\
using static_cast \nbut not recommended. \nAnd the dynamic_cast does not allow this either.\n\n\n";

//    Dog* d = new Pet; // not allowed by compiler
//    cout<< "Dog* d = new Pet;" <<"\n\n";

    Dog* d = static_cast<Dog*> (new Pet);

    cout<< "// NOT ALLOWED: \n\n";
    cout<< "Dog* d = new Pet; \n\n";
    cout<< "Dog* d = dynamic_cast<Dog*> (new Pet); \n\n\n\n";

    cout<< "//But you can use static_cast to get access to this incomplete object: \n\n";
    cout<< "Dog* d = static_cast<Dog*> (new Pet);" <<"\n\n";

    r=1111, t=2222;
    p->set_petvalue(r,t);
    cout<< "p->set_petvalue("<<r<<","<<t<<");\n\n";
    cout<< "p->pa = " << p->pa <<"\n\n";
    cout<< "p->pb = " << p->pb <<"\n\n";


    return 0;
}
Last edited on
Topic archived. No new replies allowed.