InitMyObject(MyObject* ptr)
{
*ptr = MyObject();
}
struct Data
{
MyObject obj;
};
extern Data data;
// ...
InitMyObject(&data.obj);
delete &data.obj; // ? is this ok
How I can delete (call deconstructor) data.obj
, I also try Data::obj
as pointer (nullptr default) then pass the pointer but crashed on Init.
CodePudding user response:
How I can delete (call deconstructor) data.obj
The destructor of data
will destroy its subobjects. Since data
has static storage, it is automatically destroyed at the end of the program.
delete &data.obj; // ? is this ok
No. You may only delete the result of a non-placement new expression. You may not delete a pointer to a member object.