Question

How do you specify a method to be a destructor rather than a constructor in C++? This confuses me very much. I can't tell the difference between the two.

Was it helpful?

Solution

Here's an example:

MyClass::MyClass()   // Constructor 
MyClass::~MyClass()  // Destructor

Note the "~" in front of the destructor.

OTHER TIPS

If you are planning on deriving from that class, you will need to add virtual in your .h file like so:


class MyClass
{
  MyClass();   // Constructor 
  virtual ~MyClass();  // Destructor
};

this will ensure the destructor for both the base class and the derived class is called when the derived class is destroyed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top