override Specifier In C++: Useful feature to prevent bugs

In this blog post tutorial, you will learn about C++ override specifier  (Since C++11) with the help of programming examples. This blog post explains the use of the override keyword in C++. So let’s start this blog post with a question “What is the override specifier in C++”?

 

What is the override specifier in C++?

The override specifier came along with final in the C++11 standard. The override specifies that a virtual function overrides another virtual function in a base class. Every time you define a method in the derived class that overrides a virtual method in the base class, you should tag override (virt-specifier) with this method.

General Syntax to use override specifier in C++:

Let’s see some general syntax to use the override identifier in C++ code.

 

Syntax 1:  function-declaration override pure-specifier(optional):

In a member function declaration, override appear immediately after the declarator, and before the pure-specifier, if used.

Use of override without pure-specifier

class Base
{
public:
    virtual void test();
};

class Derived : public Base
{
public:
    void test() override; //Use of override keyword
};

 

Use of override with pure-specifier:

class Interface
{
public:
    virtual int test() = 0;
};


class Base : public Interface
{
public:
    int test() override = 0; //Override with pure specifier
};


class Derived : public Base
{
public:
    int test() override
    {
        return 27;
    }
};

 

Syntax 2:  function-declaration override function-body

In a member function definition inside a class definition, the override may appear immediately after the declarator and just before the function-body.

class Base
{
public:
    virtual void f()
    {
        cout << "Base class default behaviour\n";
    }
};

class Derived : public Base
{
public:
    void f() override //override with member function has body
    {
        cout << "Derived class overridden behaviour\n";
    }
};

 

Note: If a virtual function is marked with the override and does not override a member function of a base class, the program is ill-formed. See the below example,

class Base
{
    virtual void test(int);
};


class Derived : public Base
{
    // error: wrong signature overriding Base::test
    virtual void test(long) override
    {
        //code
    }

    virtual void test(int) override // OK
    {
        //code
    }
};

 

Why should use override in C++?

The use of override keyword in C++ to help prevent inadvertent inheritance behavior in your code. Every time you define a method in the derived class that overrides a virtual method in the base class, you should marked with the override (virt-specifiers).

Let’s see examples to understand the use of the override. Also, understand why it is recommended to use override in C++. The below example shows where, without using override, the member function behavior of the derived class may not have been intended.  Also, the compiler doesn’t emit any errors for this code.

class Base
{
    virtual void test1();
    virtual void test2() const;
    virtual void test3(int);
};

class Derived: public Base
{
    virtual void test1(); // ok, works as intended


    /* Derived::test2() is non-const,
       so it does not override Base::test2() const
       and it is a new member function
    */
    virtual void test2();


    /* Derived::test3(double) has a different
      parameter type than Base::test3(int), so
      Derived::test3(double) is a new member function
      */
    virtual void test3(double);
};

Note: A function with the same name but a different parameter list as a virtual function is not necessarily virtual and does not override.

 

When you use override, the compiler generates errors instead of silently creating new member functions.

class Base
{
    virtual void test1();
    virtual void test2() const;
    virtual void test3(int);
    void test4();
};

class Derived: public Base
{
    virtual void test1() override; // ok

    /*
      compiler error: Derived::test2() does not
      override Base::test2() const
    */
    virtual void test2() override;


    /*
     compiler error: Derived::test3(double) does not
     override Base::test3(int)
    */
    virtual void test3( double = 0.0 ) override;

    /*
     compiler error: Derived::test4() does not
     override the non-virtual Base::test4()
    */
    void test4() override;
};

 

You should remember is that the override keyword is context-sensitive and has special meaning only when it’s used after a member function declaration; otherwise, it's not a reserved keyword. The below example uses override as a normal variable without any compiler warning and error.

#include <iostream>

int main()
{
    int override = 0;

    std::cout << override;

    return 0;
}

Output: 0

 

Conclusion of override specifier:

The override is used to diagnose a silent error. It serves two purposes:

1. If the member function tag with override specifier, the compiler will make sure that the member function exists in the base class. If the overridden function does not exist in the base class, it prevents the program from compiling.

The Below code will compile perfectly but it will not fulfill your need. Because int test() and int test() const are two different prototypes, and the derived class no longer overrides the method of the base class.

class Base
{
    virtual int test() const;
};

class Derived : Base
{
    virtual int test()
    {

    }
};

If you will use the override, it prevents the above code from compiling and prevent you from such type of mistake. Mostly in the case of a const member function.

2.  Increases the code readability and the reader of the code can easily understand that “this is a virtual method, that is overriding a virtual method of the base class.

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *