[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppDestructor
Every base class must have a virtual destructor.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppDestructor
(C++) Destructor
The method called when a class is destroyed. A class can have only one destructor. Opposite of the destructor is the constructor.
struct Example
{
Example() {} //Constructor
~Example() {} //Destructor
};
int main()
{
const Example e1; //Calls constructor
const Example * const pe1 = new Example; //Calls constructor
delete pe1; //Calls destructor
//As e1 goes out of scope, it will also call its destructor
}
Every base class must have a virtual destructor.
Advanced
- Declare destructors virtual in polymorphic base classes [1]
- Prevent exceptions from leaving destructors [2]
- Never call virtual functions during construction or destruction [3]
'Destructor' links
Code links
References
- 1) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 7: Declare destructors virtual in polymorphic base classes.
- 2) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 8: Prevent exceptions from leaving destructors
- 3) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 9: Never call virtual functions during construction or destruction.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
