Override specifier

Introduced by: C++11

Overriding virtual functions is one of the most important mechanisms of C++ object oriented programming. The derived class method is overriding base class method when following requirements are met:

  1.  the base class method has virtual keyword,
  2. derived class method has:
    • the same name,
    • the same input parameters,
    • the same const-ness,
    • compatible return type.

If at least one of these requirement is not fulfilled, developer can end with not-overriding method and not even know about it. The C++11 standard introduces specifier override, designed for easy detection of such mistakes.

The simply idea of overriding is that the following code should call print function from Derived class:

class Base
{
public:
virtual void print(int value);
};

class Derived : public Base
{
public:
void print(int value);
};

Base* bPointer = new Derived();
bPointer->print(5);

This code definitely will work in the intended way, but let’s take a look on a different variations of print method in Derived class:

class Derived : public Base
{
public:
// virtual void print(int value) const; // 1
// virtual bool print(int value); // 2
// virtual void print(unsigned int value); // 3
};

None of these declarations will allow the method print to be overridden (1 – constness, 2 – inconsistent return type, 3 — inconsistent parameter type). The same thing is if we forget to make print virtual in the Base class. Of course all of these mistakes are easy to do but hard to catch. There are big chances that you will not get any warnings for such implementation. It’s not incorrect – but it probably will not behave the way you wanted.

And here comes C++11 with the new override specifier!
Just declare methods in Derived class override and the compiler will check for you if there is everything ok with your overriding plans.

class Derived : public Base
{
public:
void print(int value) const override; //error!
};

You will get here compilation error since print is non-const in Base class.

This is why you should use override specifier for overriding functions in derived classes. Read what Bjarne says about it.

Override equivalent for Qt

Qt is not lagging behind C++ standard. Since Qt5 there is available macro Q_DECL_OVERRIDE which is equal to override from C++11.

Usage is also analogous, example:

void print(int value) const Q_DECL_OVERRIDE;
Advertisements

2 thoughts on “Override specifier

  1. Dybon says:

    I have a simply question if specifier keyword “override” can be put at the beginnig of the line because in my opinion it will be more readable.Please correct me when I am wrong:)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s