C + + Supplements--the principle of virtual function
Objective
C + + 's polymorphism relies on virtual functions to implement. If a class has virtual functions, each instance of the class maintains an address that points to the virtual function table. The virtual function table holds the addresses of all virtual functions in the class. Here we find the address of the virtual function table, get the address of each virtual function, and then call the virtual function directly using the address.
Body
1. Size of the empty class
#include <iostream>using namespace Std;class myclass{};int main () {cout << sizeof (MyClass) = "<< Sizeo F (MyClass) << endl;cin.get (); return 0;}
Run
An empty class does not have any members defined, and such a class size is the one that indicates that the class exists.
2. Classes with members of functions (not virtual functions)
#include <iostream>using namespace Std;class myclass{void Fun () {};}; int main () {cout << sizeof (MyClass) = "<< sizeof (MyClass) << endl;cin.get (); return 0;}
Run
Such a class: No data member, only a non-virtual function member, and its size is 1. Reason: The code area does not count toward size,
3. Classes that contain only virtual functions
#include <iostream>using namespace Std;class myclass{virtual void Fun () {};}; int main () {cout << sizeof (MyClass) = "<< sizeof (MyClass) << endl;cin.get (); return 0;}
Run
This 4 is the size of the pointer to the virtual function table.
4. virtual function table
A pointer to a virtual function table, regardless of whether the class has a data member, is located at the object's first address.
#include <iostream>using namespace Std;class myclass{public:virtual void Fun1 () {cout << "void Fun1 ()" <& Lt Endl;} virtual void fun2 () {cout << "void fun2 ()" << Endl;} virtual void Fun3 () {cout << "void Fun3 ()" << endl;}};/ /fun is a function pointer typedef void (*FUN), int main () {cout << "****** virtual function principle ***by david***" << Endl; MyClass *p = new Myclass;cout << address of virtual function table << ends;cout << (void*) * (int*) p << Endl; Fun pfun1 = (fun) * (int*) (* (int*) p) cout << (void*) pfun1 << ends;pfun1 (); Fun pfun2 = (fun) * ((int*) (* (int*) p) + 1) cout << (void*) pfun2 << ends;pfun2 (); Fun Pfun3 = (fun) * ((int*) (* (int*) p) + 2) cout << (void*) pfun3 << ends;pfun3 ();//breakpoint Cin.get (); return 0;}
Run
Information about related local variables and virtual function tables can be seen in the debug window
Below is a detailed analysis of how we get the address of the virtual function table
Union, let's take a step-by-step derivation of the address of the virtual function table and the address of each virtual function
1. The type of the pointer P is MyClass *
2. The address of the virtual function table is at the header of the object ( first four bytes ), the type of *p is MyClass, in order to obtain the storage content of the first four bytes, the P must be type-converted (int*) p
3. If * (int*) p is the address of the virtual function table, add void* because this is the standard way to print pointers in C + +.
4. Address of virtual function table * (int*) p, point to the header of the virtual function table. The address of each virtual function is four bytes in size, that is, the first four bytes of the virtual function table are stored as the address of the fun1 of a virtual function. The next four bytes of storage content is the address of the second virtual function fun2, and so on ...
5. In order to obtain the address of the FUN1, the same need for the * (int*) P type conversion (int*) (* (int*) p), it is necessary to point out that the type of * (int*) p is int*, after the reference is the FUN1 address * (int*) (* (int*) p)
6. How to get the address of fun2, of course, is the pointer to the previous step (int*) (* (int*) p) Move, move 4 bytes ( int*) (* (int*) p) +1, after the dereference * ((int*) (* (int*) p) +1 ), is the address of the fun2. Move four bytes (int*) (* (int*) p) +2 again, after the dereference * ((int*) (* (int*) p) +2), is the address of the FUN3.
Directory of this column
- C + + Supplements directory
Directory of all content
C + + Supplements--the principle of virtual function