241

I am a beginner in C++. I have come across override keyword used in the header file that I am working on. May I know, what is real use of override, perhaps with an example would be easy to understand.

einpoklum
  • 86,754
  • 39
  • 223
  • 453
SKPS
  • 4,404
  • 3
  • 21
  • 50

4 Answers4

371

The override keyword serves two purposes:

  1. It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class."
  2. The compiler also knows that it's an override, so it can "check" that you are not altering/adding new methods that you think are overrides.

To explain the latter:

class base
{
  public:
    virtual int foo(float x) = 0; 
};


class derived: public base
{
   public:
     int foo(float x) override { ... } // OK
}

class derived2: public base
{
   public:
     int foo(int x) override { ... } // ERROR
};

In derived2 the compiler will issue an error for "changing the type". Without override, at most the compiler would give a warning for "you are hiding virtual method by same name".

ruakh
  • 156,364
  • 23
  • 244
  • 282
Mats Petersson
  • 119,687
  • 13
  • 121
  • 204
  • 1
    shouldn't the function header for* foo* be similar in class *derived2* ?? I compiled this with VS2017 and got a compile error. I mean that in*derived2* *foo's* header must be: *int foo ( float x) override {...} * – Fatemeh Karimi Apr 23 '17 at 19:04
  • 51
    Uhm, that's entirely the point, the code example shows how override can be used to detect an error! – Mats Petersson Apr 24 '17 at 06:43
  • I think #3 is that override can be used to detect the to-be-overridden function (belonging to the parent class/interface) is removed (not alternated). This is useful if the subclass is expecting a callback but it will never happen due to the library change, etc. – Hei Mar 11 '18 at 05:32
  • I don't think the compiler can do that without scanning all of the call-graph, and if the compiler does that, it won't need `override` to understand that. It's really hard to achieve that. – Mats Petersson Mar 11 '18 at 07:48
75

And as an addendum to all answers, FYI: override is not a keyword, but a special kind of identifier! It has meaning only in the context of declaring/defining virtual functions, in other contexts it's just an ordinary identifier. For details read 2.11.2 of The Standard.

#include <iostream>

struct base
{
    virtual void foo() = 0;
};

struct derived : base
{
    virtual void foo() override
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

int main()
{
    base* override = new derived();
    override->foo();
    return 0;
}

Output:

zaufi@gentop /work/tests $ g++ -std=c++11 -o override-test override-test.cc
zaufi@gentop /work/tests $ ./override-test
virtual void derived::foo()
zaufi
  • 5,946
  • 21
  • 32
  • 3
    It works (here and for `final`) because you can't use regular identifiers where these contextual keywords would be placed. – CTMacUser Aug 16 '13 at 20:51
  • 1
    why the hell it's not forbidden? – Ferenc Dajka Oct 14 '16 at 09:39
  • 4
    @FerencDajka 1. Why should it be? 2. Suddenly adding a new keyword (i.e. forbidding the use of it everywhere else) would break backwards compatibility. – idmean Feb 05 '17 at 08:13
  • 1
    ordinal or ordinary ? – v.oddou Feb 21 '19 at 08:46
  • What is "The Standard"? Google has zillions of things called "Standard"... Link please? – krubo Nov 22 '19 at 12:29
  • 1
    @krubo https://isocpp.org/std/the-standard The word "standard" refers to something that is officially agreed upon internationally. For example, the definition of how long a meter is is a standard – Michael Mar 22 '20 at 07:09
10

override is a C++11 keyword which means that a method is an "override" from a method from a base class. Consider this example:

   class Foo
   {
   public:
        virtual void func1();
   }

   class Bar : public Foo
   {
   public:
        void func1() override;
   }

If B::func1() signature doesn't equal A::func1() signature a compilation error will be generated because B::func1() does not override A::func1(), it will define a new method called func1() instead.

Ramad
  • 91
  • 6
xorguy
  • 2,344
  • 1
  • 13
  • 14
  • 12
    According to C++ specification, "override" is an "identifier with special meaning". But such things in C# are called contextual keywords. – Denis Gladkiy Aug 13 '13 at 03:06
  • I think there's someting incorrect in this snippet. For the compiler to issue an error, the signature in Bar, the derived class, would have to be different, as in `void func1(int v) override;` in order for the compiler to issue an error like `marked 'override', but does not override. IOW: Oddly enough, the override seems a hint for the compiler to use in the derived class, to verify that the same signature exists in the base. See [Mats Petersson](https://stackoverflow.com/a/18198377/1724025) answer above. – Coffee_fan Dec 16 '20 at 16:10
0

Wikipedia says:

Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

In detail, when you have an object foo that has a void hello() function:

class foo {
    virtual void hello(); // Code : printf("Hello!");
}

A child of foo, will also have a hello() function:

class bar : foo {
    // no functions in here but yet, you can call
    // bar.hello()
}

However, you may want to print "Hello Bar!" when hello() function is being called from a bar object. You can do this using override

class bar : foo {
    virtual void hello() override; // Code : printf("Hello Bar!");
}
Giwrgos Tsopanoglou
  • 967
  • 2
  • 7
  • 14
  • 19
    You don't need the override identifier to do this in C++, it simply enforces that you are doing it properly. – Goose Oct 01 '14 at 22:53
  • Hi, indeed the wikipedia concept is good as a general basis, but as Goose mentions, the specifics of C++ add a twist: It's used in the derived class to help the compiler identify issues in your code, in this case, that the virtual function exists in the base. The devil is in the details. See [Marks Petersson](https://stackoverflow.com/a/18198377/1724025) comment above. – Coffee_fan Dec 16 '20 at 16:14