Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?
*/

Other Views

corner
*/

CPLUSPLUS FAQ - Are Constructors and Destructors Called Explicitly

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
corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings