C++實(shí)例(繼承類的sizeof大小問(wèn)題)

字號(hào):

一道關(guān)于繼承類的sizeof大小問(wèn)題,考試大提示: 一起來(lái)看一下吧!
    #include
    using namespace std;
    struct A{
    virtual int fun(); //4 byte
    char x; //1 byte
    };
    struct B:public A{
    short myfun(); //2 byte 不占類B空間
    union{
    unsigned short m;
    unsigned int c;
    }xx; //4 byte
    int (__stdcall *funs)(); //4 byte
    };
    union{
    unsigned short m;
    unsigned int c;
    }xx;
    short myfun();
    int (__stdcall *funs)(); //4 byte
    struct C{
    short myfun(); //2 byte
    union{
    unsigned short m;
    unsigned int c;
    }xx; //4 byte
    int (__stdcall *funs)(); //4 byte
    };
    int main()
    {
    //cout<    //cout<    //cout<    //cout<    cout < < sizeof(myfun()) < < endl;//2
    cout < < sizeof(( *funs)()) < < endl;//4
    cout < < sizeof(xx) < < endl;//4
    cout < < sizeof(A) < < endl;//8
    cout < < sizeof(B) < < endl;//16
    cout < < sizeof(C) < < endl;//8
    return 0;
    }
    //為啥sizeof(B)是16而不是18??offsetof可以看偏移
    //看深入探索C++對(duì)象模型,類中非虛函數(shù)不占空間