When a destructor of an object is called, at which point does the object cease to exist? Does it happen in the moment when its called? or after it finishes deleting the memory of the object's members inside the function? I'm mainly asking it in order to understand if it's legal to call an object's function inside its destructor or not
The destructor is called once the main function returns, i.e. after the return statement, just like local objects in any other function.
A destructor is a member of a function which is automatically called when the class is destroyed. It has the same name as the class name but is preceded by a tilde (~). Normally a destructor is used to clean-up when the class is destroyed. C++ Program 12.1 has a class which is named class_ex.
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ).
They are implicitly called at program termination for constructed external and static objects. Destructors are invoked when you use the delete operator for objects created with the new operator.
At which point of destructor call the object ceases to exist?
The lifetime of the object is ended by the call to its destructor. Within the destructor body, the sub-objects are still alive and member functions may be called. After the destructor body, the sub objects are destroyed.
if it's legal to call an object's function inside its destructor or not
It is legal.
Note however that calling a virtual function works differently than one might expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With