之前有对虚函数机制的讲解,见这

讨论是多级继承的虚函数表。

首先是没有函数覆盖的情况,继承方式如下图所示:

 

 对于实例DDerive dd;其所对应的虚函数列表是:

 

 

测试代码如下:

 

C++中的虚函数继承与虚继承_虚函数表C++中的虚函数继承与虚继承_虚函数_02View Code
class Base{
    public:
        typedef void (Base::*Fun)();
        virtual void f(){
            Fun q=&Base::f;
            cout<<"Base::f -> "<<(int)*(void**)&q<<endl;
        }
        virtual void g(){
            Fun q=&Base::g;
            cout<<"Base::g -> "<<(int)*(void**)&q<<endl;
        }
};
class Derive:public Base{
    public:
        typedef void (Derive::*Fun)();
        virtual void f1(){
            Fun q=&Derive::f1;
            cout<<"Derive::f1 -> "<<(int)*(void**)&q<<endl;
        }
        virtual void g1(){
            Fun q=&Derive::g1;
            cout<<"Derive::g1 -> "<<(int)*(void**)&q<<endl;
        }
};
class DDerive:public Derive{
    public:
        typedef void (DDerive::*Fun)();
        virtual void f2(){
            Fun q=&DDerive::f2;
            cout<<"DDerive::f2 -> "<<(int)*(void**)&q<<endl;
        }
        virtual void g2(){
            Fun q=&DDerive::g2;
            cout<<"DDerive::g2 -> "<<(int)*(void**)&q<<endl;
        }
};
int test2(){
    typedef void (*Fun)();
    Base b;
    Fun pFun=NULL;
    int *add;
    cout<<"Base-虚函数表地址:"<<(int*)&b<<" sizeof(Base)="<<sizeof(Base)<<endl;
    add=(int*)*(int*)&b;
    cout<<"虚函数表-第1个函数地址:"<<add<<" -> "<<(int*)(*add)<<endl;
    cout<<"虚函数表-第2个函数地址:"<<add+1<<" -> "<<(int*)*(add+1)<<endl;
    for(int i=0;i<2;i++)
    {
    pFun=(Fun)*(add+i);
    pFun();
    }
    cout<<"=================================="<<endl;
    Derive d;
    cout<<"Derive-虚函数表地址:"<<(int*)&d<<" sizeof(Derive)="<<sizeof(Derive)<<endl;
    add=(int*)*(int*)&d;
    cout<<"虚函数表-第1个函数地址:"<<add<<" -> "<<(int*)(*add)<<endl;
    cout<<"虚函数表-第2个函数地址:"<<add+1<<" -> "<<(int*)*(add+1)<<endl;
    cout<<"虚函数表-第3个函数地址:"<<add+2<<" -> "<<(int*)*(add+2)<<endl;
    cout<<"虚函数表-第4个函数地址:"<<add+3<<" -> "<<(int*)*(add+3)<<endl;
    for(int i=0;i<4;i++)
    {
    pFun=(Fun)*(add+i);
    pFun();
    }
    cout<<"=================================="<<endl;
    DDerive dd;
    cout<<"DDerive-虚函数表地址:"<<(int*)&dd<<" sizeof(DDerive)="<<sizeof(DDerive)<<endl;
    add=(int*)*(int*)&dd;
    cout<<"虚函数表-第1个函数地址:"<<add<<" -> "<<(int*)(*add)<<endl;
    cout<<"虚函数表-第2个函数地址:"<<add+1<<" -> "<<(int*)*(add+1)<<endl;
    cout<<"虚函数表-第3个函数地址:"<<add+2<<" -> "<<(int*)*(add+2)<<endl;
    cout<<"虚函数表-第4个函数地址:"<<add+3<<" -> "<<(int*)*(add+3)<<endl;
    cout<<"虚函数表-第5个函数地址:"<<add+2<<" -> "<<(int*)*(add+4)<<endl;
    cout<<"虚函数表-第6个函数地址:"<<add+3<<" -> "<<(int*)*(add+5)<<endl;
    for(int i=0;i<6;i++)
    {
    pFun=(Fun)*(add+i);
    pFun();
    }
    return 0;
}

 测试输出如下:

 

 然后是存在函数覆盖的情况,下图是对应的继承方式及函数覆盖情况:

 

相应的对于实例DDerive dd;其所对应的虚函数列表是:

测试代码如下:

C++中的虚函数继承与虚继承_虚函数表C++中的虚函数继承与虚继承_虚函数_02View Code
class Derive2:public Base{
    public:
        typedef void (Derive2::*Fun)();
        virtual void f(){
            Fun q=&Derive2::f;
            cout<<"Derive::f -> "<<(int)*(void**)&q<<endl;
        }
        virtual void g1(){
            Fun q=&Derive2::g1;
            cout<<"Derive::g1 -> "<<(int)*(void**)&q<<endl;
        }
        virtual void h1(){
            Fun q=&Derive2::h1;
            cout<<"Derive::h1 -> "<<(int)*(void**)&q<<endl;
        }
};
class DDerive2:public Derive2{
    public:
        typedef void (DDerive2::*Fun)();
        virtual void f(){
            Fun q=&DDerive2::f;
            cout<<"DDerive::f -> "<<(int)*(void**)&q<<endl;
        }
        virtual void g1(){
            Fun q=&DDerive2::g1;
            cout<<"DDerive::g1 -> "<<(int)*(void**)&q<<endl;
        }
        virtual void h2(){
            Fun q=&DDerive2::h2;
            cout<<"DDerive::h2 -> "<<(int)*(void**)&q<<endl;
        }
};
int test3(){
    typedef void (*Fun)();
    Base b;
    Fun pFun=NULL;
    int *add;
    cout<<"Base-虚函数表地址:"<<(int*)&b<<" sizeof(Base)="<<sizeof(Base)<<endl;
    add=(int*)*(int*)&b;
    cout<<"虚函数表-第1个函数地址:"<<add<<" -> "<<(int*)(*add)<<endl;
    cout<<"虚函数表-第2个函数地址:"<<add+1<<" -> "<<(int*)*(add+1)<<endl;
    for(int i=0;i<2;i++)
    {
    pFun=(Fun)*(add+i);
    pFun();
    }
    cout<<"=================================="<<endl;
    Derive2 d;
    cout<<"Derive-虚函数表地址:"<<(int*)&d<<" sizeof(Derive)="<<sizeof(Derive)<<endl;
    add=(int*)*(int*)&d;
    cout<<"虚函数表-第1个函数地址:"<<add<<" -> "<<(int*)(*add)<<endl;
    cout<<"虚函数表-第2个函数地址:"<<add+1<<" -> "<<(int*)*(add+1)<<endl;
    cout<<"虚函数表-第3个函数地址:"<<add+2<<" -> "<<(int*)*(add+2)<<endl;
    cout<<"虚函数表-第4个函数地址:"<<add+3<<" -> "<<(int*)*(add+3)<<endl;
    for(int i=0;i<4;i++)
    {
    pFun=(Fun)*(add+i);
    pFun();
    }
    cout<<"=================================="<<endl;
    DDerive2 dd;
    cout<<"DDerive-虚函数表地址:"<<(int*)&dd<<" sizeof(DDerive)="<<sizeof(DDerive)<<endl;
    add=(int*)*(int*)&dd;
    cout<<"虚函数表-第1个函数地址:"<<add<<" -> "<<(int*)(*add)<<endl;
    cout<<"虚函数表-第2个函数地址:"<<add+1<<" -> "<<(int*)*(add+1)<<endl;
    cout<<"虚函数表-第3个函数地址:"<<add+2<<" -> "<<(int*)*(add+2)<<endl;
    cout<<"虚函数表-第4个函数地址:"<<add+3<<" -> "<<(int*)*(add+3)<<endl;
    cout<<"虚函数表-第5个函数地址:"<<add+2<<" -> "<<(int*)*(add+4)<<endl;
    cout<<"虚函数表-第6个函数地址:"<<add+3<<" -> "<<(int*)*(add+5)<<endl;
    for(int i=0;i<5;i++)
    {
    pFun=(Fun)*(add+i);
    pFun();
    }
    return 0;
}

测试结果如下:

 

其中相同的颜色代表相同的函数