How does placement new know which layout to create?

前端 未结 4 1326
礼貌的吻别
礼貌的吻别 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:35

    Just a precision: a way to implement virtual inheritance is to push (all) the virtual base classes at the end of the classes after all the own member fields declarations. Here should be the organization of class D and class B.

    class D:
       -----
         pvmt for B, D (include shift to find A)
         int b;
       -----
         pvmt for C (include shift to find A)
         int c;
       -----
         int a;
       -----
    
    class B:
       -----
         pvmt for B (include shift to find A)
         int b;
       -----
         int a;
       -----
    
    class C:
       -----
         pvmt for C (include shift to find A)
         int c;
       -----
         int a;
       -----
    

    It is not new that chooses the layout for void* rawObject= new (storage) B;; It is the fact that you create a concrete B object. So the layout is the same than the one for contiguous - the second one.

提交回复
热议问题