虚继承 不明确继承疑问

#include<iostream> #include<string> using namespace std; class Base1 { public: Base1():i(1){} virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" << endl; } virtual void h() { cout << "Base1::h" << endl; } int i; }; class Base2:public virtual Base1{ public: virtual void f() { cout << "Base2::f" << endl; } virtual void f1() { cout << "Base2::f1" << endl; } virtual void g() { cout << "Base2::g" << endl; } //virtual void h() { cout << "Base2::h" << endl; }//取消注释该行,编译失败,因在虚基类的虚函数表中不知道该填入Base2:h还是Base3:h /*如采用 class Base1{} class Base2{} class Base3{} class Derive : public Base1,public Base2, public Base3{} 这样的结构则没此问题。*/ }; class Base3:public virtual Base1 { public: virtual void f() { cout << "Base3::f" << endl; } virtual void g() { cout << "Base3::g" << endl; } virtual void h() { cout << "Base3::h" << endl; } }; class Derive : public Base2, public Base3 { public: virtual void f() { cout << "Derive::f" << endl; } virtual void g() { cout << "Derive::g" << endl; } virtual void g1() { cout << "Derive::g1" << endl; } }; typedef void(*Fun)(void); int main() { typedef void (*Func)(); Func pFun; Derive d; int **pVtab =(int**)&d; cout << "[0] Base2->vfptr:" << endl; pFun =(Func)pVtab[0][0]; cout << " [0] "; pFun(); pFun =(Func)pVtab[0][1]; cout << " [1] "; pFun(); pFun =(Func)pVtab[0][2]; cout << " [2] "; cout << pFun << endl; cout << "[1] Base2->vbptr:" << endl; pFun =(Func)pVtab[1][0]; cout << " [0] "; cout << pFun << endl; pFun =(Func)pVtab[1][1]; cout << " [1] "; cout << pFun << endl; cout << "[2] Base3->vbptr:" << endl; pFun =(Func)pVtab[2][0]; cout << " [0] "; cout << pFun << endl; pFun =(Func)pVtab[2][1]; cout << " [0] "; cout << pFun << endl; cout << "[3] Base1->vfptr:" << endl; pFun =(Func)pVtab[3][0]; cout << " [0] "; pFun(); pFun =(Func)pVtab[3][1]; cout << " [1] "; pFun(); pFun =(Func)pVtab[3][2]; cout << " [2] "; pFun(); pFun =(Func)pVtab[3][3]; cout << " [3] "; cout << pFun << endl; cout << "[4] Base1->i:"<< *((int*)(&d)+4)<< endl; //cout << (int*)*((int*)(&d)+2)<< endl; //cout << *(int*)*((int*)(&d)+2)<< endl; //cout << (int*)*((int*)(&d)+2)<< endl; //打印地址时以十六进制打印 //cout << *((int*)(&d)+2)<< endl; system("pause"); return 0; }   

Derived的内存布局:

class Derive size(20):
 +---
 | +--- (base class Base2)
 0 | | {vfptr}
 4 | | {vbptr}
 | +---
 | +--- (base class Base3)
 8 | | {vbptr}
 | +---
 +---
 +--- (virtual base Base1)
12 | {vfptr}
16 | i
 +---
Derive::$vftable@Base2@:
 | &Derive_meta
 |  0
 0 | &Base2::f1
 1 | &Derive::g1
Derive::$vbtable@Base2@:
 0 | -4
 1 | 8 (Derived(Base2+4)Base1)
Derive::$vbtable@Base3@:
 0 | 0
 1 | 4 (Derived(Base3+0)Base1)
Derive::$vftable@Base1@:
 | -12
 0 | &Derive::f
 1 | &Derive::g
 2 | &Base3::h//不注释掉virtual void h(){ cout<<"Base2::h"<<endl;},则因该处不知填入Base2::h,还是Base3::h,而编译不了。
Derive::f this adjustor: 12
Derive::g this adjustor: 12
Derive::g1 this adjustor: 0
vbi:    class  offset o.vbptr  o.vbte fVtorDisp
           Base1      12       4       4 0

有关该问题,可查看http://topic.csdn.net/u/20091028/10/da703f45-0765-4c4c-8ed2-df287a6f86a0.html

你可能感兴趣的:(c,Class,fun)