使用Vptr的C ++派生类对象的大小

时间:2019-07-11 13:23:14

标签: object vptr

以下派生类的obj大小是多少?在64位环境中的输出是24和24。但是我希望假设每个类的虚拟表都是32和40。

能否请您说明为什么输出只有24个?

#include <iostream>

using namespace std;

class Base1
{
    virtual void fun1() { cout << "Base1::fun1()" << endl; }    
};
class Base2 {
    virtual void fun1() { cout << "Base2::fun1()" << endl; }    
};
class Base3 {
    virtual void fun1() { cout << "Base3::fun1()" << endl; }    
    };

class Derive : public Base1, public Base2, public Base3
{  
public:
    virtual void Fn()
    {
        cout << "Derive::Fn" << endl;
    }
    virtual void Fnc()
    {
        cout << "Derive::Fnc" << endl;
    }
};

class Derive1 : public Derive
{  
public:
    virtual void Fn1()
    {
        cout << "Derive::Fn" << endl;
    }
    virtual void Fnc2()
    {
        cout << "Derive::Fnc" << endl;
    }
};

int main()
{
    Derive obj;
    Derive1 Obj1;

    cout << sizeof(obj);
    cout << sizeof(Obj1);

    return 0;
}

0 个答案:

没有答案