Is it possible to call constructor and destructor explicitly in C++?
A constructor is a special member function that is automatically called by the compiler when an object is created and the destructor is also a special member function that is also implicitly called by the compiler when the object goes out of scope. They are also called when a dynamically allocated object is allocated and destroyed, a new operator allocates storage and calls constructor, deletes operator calls destructor, and frees the memory allocated by new.
Is it possible to call constructor and destructor explicitly?
Yes, it is possible to call special member functions explicitly by the programmer.
Example:
CPP
// C++ program to demonstrate an explicit call #include <iostream> using namespace std; class Test { public : Test() { cout << "Constructor is executed\n" ; } ~Test() { cout << "Destructor is executed\n" ; } }; int main() { Test(); // Explicit call to constructor Test t; // local object t.~Test(); // Explicit call to destructor return 0; } |
Output:
Constructor is executed Destructor is executed Constructor is executed Destructor is executed Destructor is executed
When the constructor is called explicitly the compiler creates a nameless temporary object and it is immediately destroyed. That’s why 2nd line in the output is called to destructor.
Here is a conversation between me and Dr. Bjarne Stroustrup via mail about this topic:
Me: Why does C++ allow to call constructor explicitly? Don’t you think that this shouldn’t be?
Dr. Bjarne: No. temporary objects of the class types are useful.
Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended [Example: if the destructor for an automatic object is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined. —end example ].
- We should never call the destructor explicitly on a local (automatic) object because really bad results can be acquired by doing that.
- Local objects are automatically destroyed by the compiler when they go out of scope and this is the guarantee of the C++ language.
In general, special member functions shouldn’t be called explicitly. Constructor and destructor can also be called from the member function of the class.
Example:
CPP
// C++ program to demonstrate an explicit call of destructor #include <iostream> using namespace std; class Test { public : Test() { cout << "Constructor is executed\n" ; } ~Test() { cout << "Destructor is executed\n" ; } void show() { Test(); this ->Test::~Test(); } }; int main() { Test t; t.show(); return 0; } |
Output:
Constructor is executed Constructor is executed Destructor is executed Destructor is executed Destructor is executed
An explicit call to destructor is only necessary when an object is placed at a particular location in memory by using placement new. Destructor should not be called explicitly when the object is dynamically allocated because the delete operator automatically calls destructor.
Example:
CPP
#include <iostream> using namespace std; class Test { public : Test() { cout << "Constructor is executed\n" ; } ~Test() { cout << "Destructor is executed\n" ; } friend void fun(Test t); }; void fun(Test t) { Test(); t.~Test(); } int main() { Test(); Test t; fun(t); return 0; } |
Output:
Constructor is executed Destructor is executed Constructor is executed Constructor is executed Destructor is executed Destructor is executed Destructor is executed Destructor is executed
This article is contributed Meet Pravasi. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above
Please Login to comment...