Override identifier after destructor in C++11

EnterTheNameHere Bohemian picture EnterTheNameHere Bohemian · Jul 29, 2013 · Viewed 38k times · Source

Does the override identifier after virtual destructor declaration have any special meaning?

class Base
{
public:
    virtual ~Base()
    {}

    virtual int Method() const
    {}
};

class Derived : public Base
{
public:
    virtual ~Derived() override
    {}

    virtual int Method() override // error: marked override, but does not override - missing const
    {}
};

Using override identifier on virtual method is useful as check: compiler will report error when the Base virtual method is actualy not overriden.

Does override on virtual destructor has any meaning/function too?

Answer

Mankarse picture Mankarse · Jul 29, 2013

Yes. If the base destructor is not virtual then the override marking will cause the program to not compile:

class Base
{
public:
    ~Base()
    {}
};

class Derived : public Base
{
public:
    virtual ~Derived() override //error: '~Derived' marked 'override' but does
                                //        not override any member functions
    {}
};