Programming in VS2013, commissioning
Question: Diamond inheritance will attract, ambiguity
1. Source Code
</pre><pre name= "code" class= "CPP" > #include <iostream>using namespace std;class base{public: virtual void Funtest () {cout << "base::funtest ()" << Endl;} virtual void FunTest1 () {cout << "base::funtest1 ()" << Endl;}}; Class C1:virtual public base{public:virtual void FunTest2 () {cout << "c1::funtest2 ()" << Endl;}}; Class C2:virtual public base{public:virtual void FunTest3 () {cout << "c2::funtest3 ()" << Endl;}}; Class Der:public C1, public c2{public:virtual void FunTest4 () {cout << "der::funtest4 ()" << Endl;} virtual void FunTest5 () {cout << "der::funtest5 ()" << Endl;} virtual void FunTest6 () {cout << "Der::funtest6 ()" << Endl;}}; typedef void (*vftab) (), void Test () {Der d;cout << sizeof (d) << endl;cout << "-------C1----" << E Ndl;int *VFPT = (int *) (* (int *) &d), Vftab vft = (vftab) (* (int *) VFPT), while (vft! = 0) {vft (); vfpt++;vft = (Vftab) (*VFP t);} cout << endl;cout << "-------C2----" << ENDL;VFPT = (int *) (* (int *) &d + 2)); vft = (Vftab) (* (int *) VFP t); while (vft! = 0) {vft (); vfpt++;vft = (Vftab) (*VFPT);} cout << endl;cout << "-------Base----" << ENDL;VFPT = (int *) (* (int *) &d + 4)); vft = (Vftab) (* (int *) VFPT); while (vft! = 0) {vft (); vfpt++;vft = (Vftab) (*VFPT);}} int main () {Test (); GetChar (); return 0;}
Operation Result:
How did these results come about? Why does D have a size of 20 bytes??
Then look at the memory of D.
Now you know why D is 20 bytes in size! But the question is, what's in the memory of d?
Take a step-by-step look at it!
It looks like these are all addresses, so let's see what these addresses have in store.
I. look at the address (0x 2a dd 2c)
You can see from monitoring 1 that the address (0x 2a 11 59) is the entry address of the virtual function c1::funtest2 ()
You can see from monitoring 1 that the address (0x 2a 12 53) is the entry address of the virtual function der::funtest4 ()
You can see from the Monitoring 1 address (0x 2a de) is the entry address of the virtual function der::funtest5 ()
You can see from monitoring 1 that the address (0x 2a 10 05) is the entry address of the virtual function Der::funtest6 ()
Summary 1: address (0x 2a dd 2c), should be class der inherit from class C1 the address of the virtual table, the virtual table stored in the Class Der and class C1 the address of the virtual function
Two. Check the address (0x 2a dd 5c)
You can see 0x 4e f9 + 0x (xx), 0c = 0x, 4e F9 3c
Summary 2: Address (0x 2a dd 5c), should be stored is offset
three. Check the address (0x 2a dd)
You can see from monitoring 1 that the address (0x 2a 10 69) is the entry address of the virtual function c2::funtest3 ()
Summary 3: address (0x 2a dd), should be class der inherit from class C2 the address of the virtual table, the virtual table stored in the virtual function of the class C2 address
four. take A look at the address (0x 2a dd)
Can see 0x 4e f9 + 0x xx xx xx = 0x xx 4e F9 3c
Summary 4: Address (0x 2a DD 68), should be stored is offset
Five. Check the address (0x 2a dd)
You can see from the Monitoring 1 address (0x 2a C6) is the entry address of the virtual function base::funtest ()
You can see from the Monitoring 1 address (0x 2a 7b) is the entry address of the virtual function base::funtest1 ()
Summary 5: Address (0x 2a dd 50), the address of the virtual table that the Class der inherits from the class base, and the virtual function address of the class base stored in the dummy table
Based on the above step-by-step analysis: You can get a model of a diamond virtual inheritance (containing virtual functions, but not overridden):
Looking at these two offsets, they ensure that class Der inherits the virtual function of class base and the uniqueness of the data members of class base, thus avoiding the creation of two semantics.
Note: The address offset in the function test () is to make it easier to get an address from memory, to see what's inside, and if each class adds its own data member, that's not the way to value it.
What am I talking about? It means:
VFPT = (int *) (* (int *) &d + 2));
VFPT = (int *) (* (int *) &d + 4));
See 2 and 4, that's what this is about.
Virtual inheritance with a virtual function diamond (No override of virtual function)