How do you define a method for a destructor instead of a constructor in C ++?

How do you define a method as a destructor, not a constructor in C ++? It bothers me a lot. I can not tell the difference between the two.

+3
source share
2 answers

Here is an example:

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

Note the "~" before the destructor.

+14
source

If you plan to extract from this class, you will need to add a virtual .h file of the file like this:


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

this ensures that the destructor for both the base class and the derived class is called when the derived class is destroyed.

+5
source

Source: https://habr.com/ru/post/1711775/


All Articles