Yes, you can; if you are using a new location as specified here ( https://isocpp.org/wiki/faq/dtors#placement-new ), which indicates the memory location for B and is documented here ( https://isocpp.org/wiki / faq / dtors # placement-new ),
But beware of destructor behavior when using this, and initializers in B will randomly overload the data. You must also be very careful about memory alignment and any requirements that objects have in this regard.
Specific warnings to consider on this page:
You are also solely responsible for destructing the placed object. This is done by explicitly calling the destructor:
ADVICE: Don’t use this "placement new" syntax unless you have to. Use it only when you really care that an object is placed at a particular location in memory. For example, when your hardware has a memory-mapped I/O timer device, and you want to place a Clock object at that memory location.
and DANGER: You are taking sole responsibility that the pointer you pass to the "placement new" operator points to a region of memory that is big enough and is properly aligned for the object type that you’re creating. Neither the compiler nor the run-time system make any attempt to check whether you did this right. If your Fred class needs to be aligned on a 4 byte boundary but you supplied a location that isn’t properly aligned, you can have a serious disaster on your hands (if you don’t know what "alignment" means, please don’t use the placement new syntax). You have been warned.
source
share