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?
static_assert(std::has_virtual_destructor<Parent>::value, "contract violated");
– milleniumbug~Child() override = default;
in a code base I might just remove the line. – juanchopanzastatic_assert
, just that it's more confusing than theoverride
version. Which is true, because it's longer, more verbose, and uses a comparatively obscure feature of the standard library. – Kyle Strand