Monday, 17 November 2014

How is it possible to call constructor and destructor explicitly? Why we need to call them explicitly?

How is it possible to call constructor and destructor explicitly? Why we need to call them explicitly?

Introduction to Constructor:
Constructor is also a member function in the class, If we forgot to declare the constructor the C++ compiler will automatically provide on default constructor for us. The constructor is called when the object is created for the class.

Introduction to Destructor:
Destructor is also a member function in the class, If we forgot to declare the destructor the C++ compiler will automatically provide on default destructor for us. The constructor is called when the object is goes out of the class scope. The destructor is used to destroy the heap memory that is used for the member variable of the class.

Is it possible to call constructor and destructor explicitly?

For this question, let us see the below example which will help us to understand more about the question.

#include <iostream>
using namespace std;
class myClass
{
public:
    myClass ()  {
      cout << "I am in the constructor of the class\n";
    }
    ~myClass () {
      cout << "I am in the destructor of the class\n";
    }
};
int _tmain (int argc, _TCHAR* argv[])
{ 
   myClass ();  // Explicit call to constructor
   myClass ocClas; // creation of the object for the class
   ocClas.~myClass(); // Explicit call to destructor
   getchar ();
   return 0;
}

The ouput of the above program is
I am in the constructor of the class
I am in the destructor of the class
I am in the constructor of the class
I am in the destructor of the class

In the above case when the constructor is called explicitly the compiler creates a nameless temporary object and it is immediately destroyed, so the destructor gets called.

For that reason, the second line in the output is call to destructor.

Why it is possible the explicit calling of constructor without the object creation??

All the local objects are destroyed automatically by the compiler when the class goes out of scope. In general, special member functions shouldn’t be called explicitly.
Constructor and destructor can also be called from the member function of class. See following program:
#include <iostream>
using namespace std;
class myClass
{
public:
    myClass ()  {
      cout << "I am in the constructor of the class\n";
    }
    ~myClass () {
      cout << "I am in the destructor of the class\n";
    }
    void Display ()  {  myClass();  this->myClass::~myClass ();  }
};
int _tmain (int argc, _TCHAR* argv[])
{ 
   myClass ();  // Explicit call to constructor
   myClass ocClas; // creation of the object for the class
   ocClas.Display (); // calling the constructor through the member function of the class
   ocClas.~myClass(); // Explicit call to destructor
   getchar ();
   return 0;
} }

The ouput of the above program is

I am in the constructor of the class
I am in the destructor of the class
I am in the constructor of the class
I am in the constructor of the class
I am in the destructor of the class
I am in the destructor of the class
I am in the destructor of the class 

Calling the destructor explicitly is required only at the situation where the class is used the heap memory (i.e, the object is placed at the memory location using the new operator) using the delete operator we have to delete the allocated memory. The delete operator will automatically calls the destructor of the class


No comments: