Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Destructor

 

A destructor is a class element that is called when a class goes out of scope. The class element that is called when a class is created is called the constructor.

 

#include <iostream>

struct MyClass
{
  ~MyClass() { std::cout << "Destructor\n"; }
};

int main()
{
  MyClass m;
} //MyClass destructor called at end of main

 

 

All base classes must have a (public) virtual destructor. A non-base class should have a (non-public) non-virtual destructor [1].

 

 

 

 

 

References

 

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 50: 'Make base class destructors public and virtual, or protected and nonvirtual'.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict