It is difficult for the compiler to know whether an object is allocated on a stack or a heap. On invoking the delete this operator, the destructor is called twice on the object related to the current context. Firstly implicitly and secondly explicitly.
It results in undefined behavior and error-prone conditions.
The delete this operator can only work on three conditions:
i. An Instantiation of a local variable should not take place, after calling the delete this destructor.
ii. The this pointer should not be used after calling the delete this operator.
iii. The object of a derived class should be allocated and initialized with the new operator.
No comments:
Post a Comment