<<< Chain of Destructors     Index     Using destructors >>>

14. Destructor example

  • 
    // point.h
    class Point
    {
        public:
            ~Point(); // destructor
        private:
            int m_x;
            int m_y;
    };
    
    // point.cpp
    #include <iostream>
    #include "point.h"
    Point::~Point()
    {
        std::cout << "Goodbye...";
    }
    
    // main.cpp
    #include "point.h"
    int main()
    {
        Point pt;
        return 0;
    }// ~Point() destructor invoked
    
    
  • ~Point() is the destructor for class Point.

  • Destructors are invoked when an object goes out of scope -- you don't call the destructors yourself !

  • Destructors

    • do not take args (so they cannot be overloaded)

    • do not return anything


<<< Chain of Destructors     Index     Using destructors >>>