Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default override of virtual destructor

Tags:

c++

c++11

c++14

Everyone knows that the desructor of base class usually has to be virtual. But what is about the destructor of derived class? In C++11 we have keyword "override" and ability to use the default destructor explicitly.

struct Parent {   std::string a;   virtual ~Parent()   {   }  };  struct Child: public Parent {   std::string b;   ~Child() override = default; }; 

Is it correct to use both keywords "override" and "=default" in the destructor of Child class? Will compiler generate correct virtual destructor in this case?

If yes, then can we think that it is good coding style, and we should always declare destructors of derived classes this way to ensure that base class destructors are virtual?

like image 399
Sandro Avatar asked Dec 06 '16 16:12

Sandro


People also ask

Can we override destructor in C++?

A pure virtual destructor can be declared in C++. After a destructor has been created as a pure virtual object(instance of a class), where the destructor body is provided. This is due to the fact that destructors will not be overridden in derived classes, but will instead be called in reverse order.

Are destructors virtual by default?

The destructor is not virtual (that is, the base class destructor is not virtual) All direct base classes have trivial destructors.

Should virtual destructors be marked override?

As long as the base class has the destructor marked as virtual, all derived class destructors will call the basal destructor correctly. In your case it doesn't matter much. But just in case you're actually using virtual functions yourself in your other classes, you should stick with the override keyword.

What is the default destructor in C++?

The default destructor calls the destructors of the base class and members of the derived class. The destructors of base classes and members are called in the reverse order of the completion of their constructor: The destructor for a class object is called before destructors for members and bases are called.


1 Answers

Is it correct to use both keywords "override" and "=default" in the destructor of Child class? Will compiler generate correct virtual destructor in this case?

Yes, it is correct. On any sane compiler, if the code compiles without error, this destructor definition will be a no-op: its absence must not change the behavior of the code.

can we think that it is good coding style

It's a matter of preference. To me, it only makes sense if the base class type is templated: it will enforce a requirement on the base class to have a virtual destructor, then. Otherwise, when the base type is fixed, I'd consider such code to be noise. It's not as if the base class will magically change. But if you have deadheaded teammates that like to change things without checking the code that depends on what they may be possibly breaking, it's best to leave the destructor definition in - as an extra layer of protection.

like image 127
Kuba hasn't forgotten Monica Avatar answered Sep 19 '22 21:09

Kuba hasn't forgotten Monica