Why does multiple inheritance increase the size of the object despite the bases being empty?

后端 未结 2 745
一个人的身影
一个人的身影 2021-01-17 09:06

Given this code:

#include 

struct A {

};

struct B {

};

struct C {

};

struct E : A {
    int field;
};

struct F : A, B {
    int field         


        
2条回答
  •  再見小時候
    2021-01-17 09:34

    There is no language rule that says that any particular type needs to have any particular size, with the exception of char (size 1), and subject to the constraint that complete objects of class type have non-zero size. There is nothing buggy about your particular compiler's way of laying out the types in your example.

    As for the new question, after you edited it: It's possible that MSVC just doesn't put a lot of effort into optimising multiple inheritance, because that's a comparatively rare thing that you could argue that there's little pay-off. I don't know anything about the real decision process that went on, but just consider that there may be pragmatic engineering trade-offs like this at play.

提交回复
热议问题