What is the 'override' keyword in C++ used

2019-01-07 03:00发布

This question already has an answer here:

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.

4条回答
Explosion°爆炸
2楼-- · 2019-01-07 03:09

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!");
}
查看更多
闹够了就滚
3楼-- · 2019-01-07 03:15

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

// kate: hl C++11;
#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()
查看更多
叼着烟拽天下
4楼-- · 2019-01-07 03:24

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 { ... do stuff with x and such ... }
}

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

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".

查看更多
贼婆χ
5楼-- · 2019-01-07 03:27

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.

查看更多
登录 后发表回答