Does calling a destructor explicitly destroy an object completely?

前端 未结 11 1433
日久生厌
日久生厌 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:12

    Yes. A destructor calls any member destructors in LIFO order, then base class destructors, and there is no way to prevent it from calling these destructors*. The object stack is guaranteed to unwind.

    Initialization and finalization are separated from memory allocation and deallocation in C++ exactly so that when the special case arises, there is an unambiguous syntax in which the application-programmer can express his or her intent to the compiler.

    Edit:

    • I suppose that by invoking abort() or longjmp() one could, in fact, prevent the member and base class destructors from running.

提交回复
热议问题