How does placement new know which layout to create?

前端 未结 4 1367
礼貌的吻别
礼貌的吻别 2021-01-25 01:04
#include 
#include 
struct A { int a; };
struct B : virtual A { int b; };
struct C : virtual A  { int c; };
struct D : B,C { int d; };
in         


        
4条回答
  •  耶瑟儿~
    2021-01-25 01:46

    There is no option to create what you call a "separate" layout, other than creating a derived type, and fishing B out of it.

    "Layout of B as portion of its derived class" is not the same as "Layout of B". Placement new and regular new use the layout based on the type itself, with is the default, stand-alone layout.

    Where is the guarantee that the layout created by new fits into a buffer of size sizeof(B)?

    sizeof(B) returns the size of B itself, not B-as-part-of-some-other-class. That is all the space needed to store a stand-alone B, regardless of the way that you allocate memory for it.

提交回复
热议问题