How does placement new know which layout to create?

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

    There are two things happening in a new expression:

    1. Allocating memory for a type by calling operator new (which is no new expression)

    2. Construction of the desired type.

    Usually the operator new can utilize the size, which is passed by the compiler inside the new expression. Having a placement new (offering storage), the programmer is responsible to provide a storage large enough to hold the desired type.

    Anyway: Do not mix allocation and object construction. These are two distinct tasks.

提交回复
热议问题