Are constructors and destructors called explicitly?
Constructors and Destructors are not called explicitly ie they are not called with a code using code syntax like other functions; however when there is an object created and destroyed, they are called implicitly or automatically behind the scenes.
If the programmer provides a constructor for a class, the compiler will arrange to call the constructor automatically when an object of the class is created. Similarly, if the programmer provides a destructor for a class, the compiler will call it automatically when an object of the class is destroyed.
Here is an example where Constructors and Destructors are called explicitly.
int main()
{
Student s1; // s1's constructor called
{
Student s2; // s2's constructor called
} // s2's destructor called
return 0; // s1's destructor called
}
C++ FAQ Home