2

I know that the following function is a virtual function and needs to be overridden when extended by another class:

virtual int getAge()=0;

What i don't understand is the following syntax I have seen in places:

double getAge(int id) override;

Is this also a virtual function? I thought the word "override" is only used when overriding an extended virtual function.

0

2 Answers 2

18

C++ has a rule about overridden functions: they don't have to be explicitly declared as virtual. Consider your example without override: double getAge(int id);. If there is a base class function declared virtual, named getAge, and takes the same parameters as the derived class function, then the derived class function will implicitly be virtual and it will be an override for that function.

However, what happens if you make a mistake? Let's say that you mistype the derived function name as gtAge. Well, if there isn't an override-able base class function... then that will declare a non-virtual function which overrides nothing. It remains a perfectly valid function declaration. The user made a mistake, but the compiler had no way to know. Even adding the virtual keyword doesn't solve this, as it's perfectly valid to add new virtual functions to a derived class.

Now, in your specific case, compilation will still fail. You declared the base class function as pure-virtual, so a derived class which doesn't override it cannot be instantiated. But even then, the error won't point directly at the line causing the problem; it won't point at the gtAge. Instead you'll get a "cannot instantiate due to no overridden getAge" error; the user will have to figure out where the derived class failed to override this function.

But if the base class function was not pure-virtual, then you're completely out of luck. You'll get highly unexpected behavior and it will take a while to figure out where you went wrong.

Enter override. This tells the compiler that you specifically intend this declaration to override a base class virtual function. So if the compiler doesn't find a matching function in a base class, the compiler will error out immediately, telling you exactly where you went wrong.


You may be getting confused by the fact that double getAge(int id) actually returns a different type from what it overrides. This is actually (unfortunately, in my opinion) perfectly valid. Overridden return types can be any type which is convertible to the return type of the base class function being overridden.

4
  • 4
    +1 for catching and explaining the differing return types.
    – Erik Eidt
    Commented Jan 20, 2019 at 20:32
  • Great answer! When did this appear in C++ (which standard)?
    – Cedric H.
    Commented Jan 24, 2019 at 19:24
  • 1
    @CedricH.: C++11. Commented Jan 24, 2019 at 19:30
  • 1
    Well, some leeway with the return-type makes sense with returning pointers / references if you want to strengthen the contract. Here it sure is confusing. Commented Jul 7, 2019 at 19:50
3
double getAge(int id) override;

Is this also a virtual function?

Yes, in at least two senses:

First, invocation of this method using the regular syntax (instance method invocation) uses virtual dispatch.  The caller could be invoking the base class's method (e.g. by using an expression that refers the base class type as the dynamic dispatch target), or the caller could be invoking the subclass's method (e.g. by using an expression referring to the subclass), either way it is a virtual dispatch.

Second, the subclass's method can be overridden in yet another subclass (a subclass of the subclass), so in that sense the override is also a virtual method (as it can be overridden).

No, however, in another sense: the overriding method does not introduce a new virtual instance method (group) as that has been done already by the base class.  In terms of vtable slots, it is the virtual method in the base class that gets a new vtable slot, whereas the override shares that same vtable slot (and the override is basically added to the group of instance methods associated with the originally introduced virtual method and its vtable slot).

Also note that C++11 first supports the override term.  (Technically it is not a reserved word or keyword, but instead recognized in just that one position).  Use of this term override is optional (and not supported earlier C++ versions) — when we use it we get some readability plus the support of compiler checking that we're doing what we think we're doing (which is making an override of a virtual method introduced in a base class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.