Does calling a destructor explicitly destroy an object completely?

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

    Running the destructor does not free memory used by the object being destructed - the delete operator does that. Note, however, that the destructor may delete "child objects" and their memory will be freed as per usual.

    You need to read up on placement new/delete as this allows you to control memory allocation and when constructors/destructors run.

    See here for a little info:

    http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9
    

提交回复
热议问题