class with virtual functions takes more space

前端 未结 1 1331
无人共我
无人共我 2020-12-19 16:08

There is such code:

#include 

class A{
    int a;
    int fun(){}
};

class B{
    int a;
    virtual int fun(){}
};

int main()
{
    std::         


        
相关标签:
1条回答
  • 2020-12-19 16:21

    Any class with a virtual function needs a pointer to the vtable for that class. Therefore, there is a hidden member that's the size of the pointer.

    http://en.wikipedia.org/wiki/Virtual_method_table

    0 讨论(0)
提交回复
热议问题