-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path12-vtable-override.cpp
More file actions
104 lines (83 loc) · 2.79 KB
/
12-vtable-override.cpp
File metadata and controls
104 lines (83 loc) · 2.79 KB
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
/*
Virtual Table or Virtual Method Table
In OO (object-oriented) world, a polymorphism makes Derivate class
can override the behavior of functions inherited from Base.
C++ use virtual table to implement this.
What will happen in vtable if a method is overridden?
testing on:
- TDM GCC 5.1.0 (assuming x64)
Compile:
$ g++ vtable-override.cpp -std=c++11 -o vtable-override
Run:
$ vtable-override
We can also ask compiler to emit the vtable and other structure, see the difference.
(GCC)
$ g++ -g -fdump-class-hierarchy -std=c++11 vtable-override.cpp
(MSVC)
$ cl.exe /d1 reportAllClassLayout vtable-override.cpp
*/
#include <iostream>
#include "util.hpp"
/*
Observe the address of each of them.
Questions:
- Is there any two or more entry in vtable that has same address?
- Why?
- Is there any two or more classes that has same address on vtable?
- Why?
- What happen when you uncomment the Derivate::B() ?
Conclusion:
The entry will be similar unless overriding occurred.
*/
//======== Type Definitions =========================================
/*
Memory layout:
- Base::vtable (pointer to vtable of Base)
- Base::id
The vtable layout:
- Base::~Base() (base object destructor)
- Base::~Base() (deleting destructor)
- Base::B()
- Base::C()
*/
class Base
{
public:
int id;
Base(int id) : id(id) { }
virtual ~Base() { }
void A() { std::cout << "- Base[" << id << "]::A" << std::endl; }
virtual void B() { std::cout << "- Base[" << id << "]::B" << std::endl; }
virtual void C() { std::cout << "- Base[" << id << "]::C" << std::endl; }
};
/*
Memory layout:
- Derivate::vtable (pointer to vtable of Derivate)
- Base::id
The vtable layout:
- Derivate::~Derivate() (base object destructor)
- Derivate::~Derivate() (deleting destructor)
- Base::B()
- Derivate::C()
*/
class Derivate final : public Base
{
public:
Derivate(int id) : Base(id) { }
~Derivate() { }
// void B() override { std::cout << "- Derivate[" << id << "]::B" << std::endl; }
void C() override { std::cout << "- Derivate[" << id << "]::C" << std::endl; }
};
//======== Helper Functions =========================================
//======== Main Function ============================================
int main()
{
Base base(1);
Derivate derivate(2);
Base *pbase = new Derivate(3);
dump_instance("instance of base", base, 4);
dump_instance("instance of Derivate", derivate, 4);
dump_instance("instance of pointer to Derivate", *pbase, 4);
// Try to call B() of each instance!
return 0;
}