I am reviewing a section of C++ code an I came accross this statement block:
static void Vector3DefaultConstructor(Vector3 *self)
{
new(self) Vector3();
}
I have not come accross the new operator being used in this way before. Can someone explain why new is being called in this way?
This is called “placement
new“. By default, it does not allocate memory but instead constructs the object at the given location (here,self). It can, however, be overloaded for a class.See the FAQ for more info.
The correct way to destroy an object constructed using placement
newis by calling the destructor directly: