Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the 'override' keyword just a check for a overridden virtual method?

People also ask

What does the override keyword do?

The override keyword serves two purposes: It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class." 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.

Which keyword is used to override a virtual method?

The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class. The new keyword is used to hide a method, property, indexer, or event of base class into derived class.

Can you override a virtual method?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

Is virtual method mandatory to override?

Yes, you need to use the override keyword, otherwise the method will be hidden by the definition in the derived class.


That's indeed the idea. The point is that you are explicit about what you mean, so that an otherwise silent error can be diagnosed:

struct Base
{
    virtual int foo() const;
};

struct Derived : Base
{
    virtual int foo()   // whoops!
    {
       // ...
    }
};

The above code compiles, but is not what you may have meant (note the missing const). If you said instead, virtual int foo() override, then you would get a compiler error that your function is not in fact overriding anything.


Wikipedia quote:

The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not, the compiler will error out.

http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

Edit (attempting to improve a bit the answer):

Declaring a method as "override" means that that method is intended to rewrite a (virtual) method on the base class. The overriding method must have same signature (at least for the input parameters) as the method it intends to rewrite.

Why is this necessary? Well, the following two common error cases are prevented:

  1. one mistypes a type in the new method. The compiler, unaware that it is intending to write a previous method, simply adds it to the class as a new method. The problem is that the old method is still there, the new one is added just as an overload. In this case, all calls towards the old method will function just as before, without any change in behavior (which would have been the very purpose of the rewriting).

  2. one forgets to declare the method in the superclass as "virtual", but still attempts to re-write it in a subclass. While this will be apparently accepted, the behavior won't be exactly as intended: the method is not virtual, so access through pointers towards the superclass will end calling the old (superclass') method instead of the new (subclass') method.

Adding "override" clearly disambiguates this: through this, one is telling the compiler that three things are expecting:

  1. there is a method with the same name in the superclass
  2. this method in the superclass is declared as "virtual" (that means, intended to be rewritten)
  3. the method in the superclass has the same (input*) signature as the method in the subclass (the rewriting method)

If any of these is false, then an error is signaled.

* note: the output parameter is sometimes of different, but related type. Read about covariant and contravariant transformations if interested.


Found "override" is useful when somebody updated base class virtual method signature such as adding an optional parameter but forgot to update derived class method signature. In that case the methods between the base and the derived class are no longer polymorphic relation. Without the override declaration, it is hard to find out this kind of bug.


Yes, this is so. It's a check to make sure one doesn't try an override and mess it up through a botched signature. Here's a Wiki page that explains this in detail and has a short illustrative example:

http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final


C++17 standard draft

After going over all the override hits on the C++17 N4659 standard draft, the only reference I can find to the override identifier is:

5 If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed. [ Example:

struct B {
  virtual void f(int);
};

struct D : B {
  virtual void f(long) override; // error: wrong signature overriding B::f
  virtual void f(int) override;  // OK
}

— end example ]

so I think that possibly blowing up wrong programs is actually the only effect.