Size of class with virtual function

前端 未结 1 399
感动是毒
感动是毒 2021-01-28 04:26

I was revising the C++ concepts, but I am stuck with a very simple code

#include 
using namespace std;

class foo {
public:
    //int i;
    void         


        
相关标签:
1条回答
  • 2021-01-28 05:12

    You're working on a platform where pointers are aligned to 8 bytes. Since the virtual table pointer is typically the first thing in the layout of an object, it too must be aligned to 8 bytes. So padding 4 bytes are inserted after the int member, that's why you get a size of 16 (8 bytes for the vf table pointer, 4 for the int and 4 padding bytes).

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