Does calling a destructor explicitly destroy an object completely?

前端 未结 11 1437
日久生厌
日久生厌 2020-12-03 14:43

If I call a destructor explicitly ( myObject.~Object() ) does this assure me that the object will be appropriately destroyed (calling all child destructors) ?

Ok so

11条回答
  •  爱一瞬间的悲伤
    2020-12-03 15:17

    Calling the destructor is fine. However, beware of the type you're calling it on. If that type doesn't have (didn't inherit) a virtual destructor, you might get unexpected behaviour.

    Also, as mentioned, the destructor does not free any memory, but I guess that's the reason you want to call it manually in the first place.

    Plus, unless I'm mistaken, calling the destructor manually is the only option you have if you used placement new to call the constructor.

提交回复
热议问题