Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

At which point of destructor call the object ceases to exist?

Tags:

c++

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

like image 226
Itamar Ivri Avatar asked Feb 16 '21 13:02

Itamar Ivri


People also ask

Is the destructor called at the end of Main?

The destructor is called once the main function returns, i.e. after the return statement, just like local objects in any other function.

When an object is destroyed destructor is automatically called?

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.

What happens when the destructor is called?

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 ( ~ ).

Is destructor called at end of program?

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.


1 Answers

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.

like image 93
eerorika Avatar answered Oct 21 '22 16:10

eerorika