-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinding.cpp
More file actions
60 lines (51 loc) · 1.91 KB
/
Binding.cpp
File metadata and controls
60 lines (51 loc) · 1.91 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
// when a function is called in the code, binding decides which function gets executed based on the context, such as object type/function signature
// early binding: happens during compilation, faster because everything is decided early
// late binding: happens with virtual functions where the exact function to call is decided at runtime, depending on the actual object type
#include <iostream>
using namespace std;
class base
{
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
}
};
class derived : public base
{
public:
// we have created virtual function print() in base class, and it's being overridden in derived class
// then we don't need a virtual keyword here, print() is automatically considered a virtual function
void print()
{
cout << "print derived class\n";
}
void show()
{
cout << "show derived class\n";
}
};
int main()
{
base *bptr;
derived d;
bptr = &d;
// Virtual function, binded at runtime, when the actual object type is derived
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
return 0;
}
/* How does the compiler perform runtime resolution?
the compiler maintains two things:
1. vtable: a table of function pointers, maintained per class
2. vptr: a pointer to vtable, maintained per object instance
the compiler adds additional code at two places to maintain and use vptr:
1. code in every constructor. this code sets the vptr of the object being created and sets vptr to point to the vtable of the class
2. code with polymorphism function call. 多态function被called -> compiler inserts code用base class pointer/reference找vptr -> access vptr指向的vtable -> 可以access vtable里的function address
e.g. bptr指向derived class -> derived class vptr -> derived class vtable -> derived class print()
*/