C++

Is it Possible to Call Constructor and Destructor Explicitly in C++?

In C++, a certain class of member function known as a constructor is called during the creation of an object. An object’s destructor, in contrast, is an object’s member function which is invoked whenever it has to be destroyed. Constructors are implemented to initialize objects and allocate resources, whereas destructors are used when objects are destroyed to free resources and execute cleaning tasks.

The explicit calling of C++ constructors and destructors will be covered in this article.

Can We Explicitly Call the Constructor and Destructor in C++?

The answer is Yes! We can explicitly invoke the special member functions constructor and destructor in C++.

What are Constructors in C++?

In C++, the constructor is referred to as a member function of the class which has the same name as the class that is triggered when an instance of the identical class gets generated automatically. Constructors can include arguments that allow the initialization procedure to be customized.

Syntax

The general syntax for creating a constructor function in a class is provided below:

class construct
{
    public:
    // Constructor
    construct()
    {
        // Object declaration
    }
};

Here, the access specifier and constructor name are the same as the class name, and required parameters (none in this case), and the constructor body is surrounded by curly brackets.

What are Destructors in C++?

A destructor is known as a specific kind of a member function that is immediately called Whenever an object of a class is destroyed. It allows the cleaning and deallocation of any resources assigned by the object, like memory, and file handles.

Syntax

A destructor’s syntax looks similar to the constructor, except that the class name is preceded with a tilde (~) symbol:

class D {
public:
  // Constructor
  D();
  // Destructor
  ~D();
};

A destructor has no arguments and no return type.

Example 1: An Explicit Constructor and Destructor Call

The below-demonstrated code performs an explicit call of constructor and destructor:

#include <iostream>

using namespace std;

class space{

public:

  space() { cout << "constructor execution\n"; }

  ~space() { cout << "destructor execution\n"; }

};

int main()

{

  // explicit call of the constructor

  space();

  cout<<endl;

  space s;

  cout<<endl;

  // explicit call of destructor

  s.~space();

  return 0;

}

The above program declared the “space” class, which has constructors and destructors that display messages to the command prompt. In the “main()” function, an object of the “space” class has been created explicitly and implicitly. Then, the destructor is called explicitly.

Here, check out the output of the above-described code:

Example 2: Explicit Constructor and Destructor Call for Two Objects

Let’s take another example that shows the explicit calling of the constructor and destructor. First, created a class named “Sub” with constructors and destructors. It also created a friend function called “see()” that accepts an object created by the “Sub” class as a parameter. The code used the constructor to build two “Sub” objects, then runs the “see()” function using one of them as an input. Lastly, print messages to show when the constructor and destructor are executed:

#include <iostream>

using namespace std;

class Sub{

public:

  Sub() { cout << "Constructor execution\n"; }

  ~Sub() { cout << "Destructor execution\n"; }

  friend void see(Sub s);//friend functions takes an argument object of class Sub

};

void see(Sub s)//declaration of friend function

{

  Sub();//constructor is called

  s.~Sub();// destructor is called

}

int main()

{

  Sub();// explicit call of the constructors for first object creation

  cout<<endl;

  Sub s;//explicit call of the constructors for second object creation

  cout<<endl;

  see(s);

  return 0;

}

Output

That’s it! We have elaborated on the explicit calling of constructors and destructors in C++.

Conclusion

Yes, developers can explicitly call the constructor and destructor in C++. Calling the constructor and destructor explicitly is usually unnecessary because they are called automatically as an object is created and destroyed. However, in some cases, such as manually managing memory and handling resources that must be free, explicit constructor and destructor calls may be required. This guide described the explicit calling of constructors and destructors in C++.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.