As you know, if you want to delete through a pointer to the parent class, the class must have a virtual destructor in order for the delete operator to call the correct destructor of the child class.

But what if I personally do not need a destructor in any of the classes?
Are auto-generated destructors interchangeable?
Or do I still have to add a virtual destructor to the parent class?

As far as I understand, an empty destructor is generated, so nothing will happen when the object is destroyed. Anyway, the compiler is smart and the call to the empty function will be thrown out, respectively, no destructor will be called when deleting.

In this case, it seems strange to add a virtual function, which will be called if this function is nop .

https://ideone.com/8ldKsU

 struct A { int x; A(unsigned x) : x(x) {} }; struct B : A { B() : A(7) {} }; int main() { A *a = new B(); delete a; return 0; } 
  • Question on enSO - Virtual Default Destructors in C ++ - user227465
  • one
    Read carefully the quote that Ant brought - everything is there. You essentially have two questions about the same thing. A virtual destructor can only become with inheritance or explicit instructions, no more. The compiler generates a destructor that must remove all objects, i.e. "Empty" is not true in the general case. But it's all lyrics, because The code in question gives undefined behavior according to the standard. - ixSci
  • one
    To do this, the question must undergo a rediscovery procedure. I believe that this is not necessary, and the answer is in another question. If users find it different, they will open the question. But you yourself know this very well. - ixSci
  • one
    Yes, I closed it and consider it to be correct. The answer to the question is in another question. Your code has undefined behavior. There is nothing more to talk about. You already have 3 questions on the same topic. Why produce them when you can just read one quote from Ant, given at the very beginning of his answer? - ixSci
  • one
    @ixSci, write the answer? - Qwertiy

2 answers 2

The answer to this question essentially coincides with this answer . A virtual destructor can only become with inheritance or explicit instructions, no more. The compiler generates a destructor that must remove all objects, i.e. "Empty" is not true in the general case. There are no exceptions in the standard for this case, the code in question gives undefined behavior according to the standard.

Additionally, the destructor, empty from the point of view of the source code, is in fact not empty, the logic of deallocating an instance of the class is embedded in it. Therefore, destructors of different classes are essentially different, not interchangeable.

    As you know, if you want to delete through a pointer to the parent class, the class must have a virtual destructor in order for the delete operator to call the correct destructor of the child class.

    If it came to virtual functions and to the removal of an object by a pointer to the base class, then it is recommended to write virtual destructors, even if they are empty. Then less fuss will add them.

    Again, any program tends to become a library, and as such, the following users begin to produce their classes from your classes. This is where the virtual destructor comes in handy.

    As for the above example, there are neither virtual functions nor the NECESSITY to delete the class object according to the pointer to the base class. As an illustration of the fact that in C ++ you can do without virtual destructors, such an example has the right to exist. But in real code with a more or less developed system of class hierarchies, virtual destructors must have.