Explicit override of virtual function
Asked Answered
U

4

6

I just discovered that C++/CLI has a keyword that is not present (AFAIK) on standard C++: override.

I don't know much about C++/CLI, so, can someone explain for which purpose is it included there, and if is it a desirable feature to be added to C++?

Unpriced answered 28/1, 2011 at 19:42 Comment(2)
You didn't say what the keyword you're thinking of is. Is it "explicit"? If so, it is in standard C++.Siskind
@Wes Hardaker Actually I said. It's the "override" keyword.Unpriced
C
12

override is a special keyword extension from Microsoft that can be used in C++/CLI and Visual C++ implementations. It is similar to the @Override annotation in Java or override in C#, and provides better compile time checks just in case you didn't override something you meant to.

From the first link:

override indicates that a member of a managed type must override a base class or a base interface member. If there is no member to override, the compiler will generate an error.

override is also valid when compiling for native targets (without /clr). See Override Specifiers and Native Compilations for more information.

override is a context-sensitive keyword. See Context-Sensitive Keywords for more information.

As of the C++11 standard, the override specifier is now a standardized keyword. Support is still limited, and as per this page from Apache StdCxx, override is supported by GCC 4.7+, Intel C++ 12.0+, and Visual C++ 2012 (with pre-standardization support in Visual C++ 2005).

Carborundum answered 28/1, 2011 at 19:48 Comment(0)
R
2

It helps the compiler catch your mistakes two ways:

  1. If you declare a function with override in your class, but the base class doesn't have that function, then the compiler can tell you that you're not overriding what you thought you were. If override weren't available, then the compiler wouldn't be able to recognize your error — it would simply assume that you intended to introduce a new function.

  2. If you have a function in your descendant class (without override), and then you declare that same function as virtual in the base class, the compiler can tell you that your change in the base class has affected the meaning of the original declaration in the descendant. The descendant will either need to use override, or you'll need to change the signature of one of the functions.

This feature is being added to C++0x already.

Room answered 28/1, 2011 at 20:7 Comment(1)
I don't think #2 is true. You can override functions just fine without actually using the override keyword. A lot of existing code would break if this ever changed.Hickman
S
0

From here:

If you flag a property or a method in a parent class with the virtual keyword, when deriving a class from it, you can ignore the method or property and not implement it. But if you decide to implement the property or method, you must indicate that you are providing a new version of the property or method. Providing a new version of a property or a method is referred to as overriding it.

When overriding a property or a method, you must indicate this by writing the override keyword to its right.

Now follow the link to see some examples.

Slaughterhouse answered 28/1, 2011 at 19:49 Comment(2)
That explains the mechanics of the override keyword. It does not explain why it was introduced.Haleigh
Like Rob's #2, this isn't true. Matching the signature of a function declared virtual in a base class is sufficient to override a function, the override keyword can be omitted and the function will still be overridden. New keywords can't be made mandatory without breaking huge amounts of existing code.Hickman
B
0

The override keyword in C++/CLI comes from .Net and not a part of C++ itself. Since override has already been explained, you need to know the alternative. If you do not "override" it, you may want to make it "new". By making it "new" you are not overriding the parent class' member in the child but creating a new member with the same name. The new and override keywords only differ when you use a base class pointer and point it to a derived class object.

So, if you use a base class pointer and point to a derived class object:

If you call an "override"n member:
      the derived class member is called
if you call the "new"ed member:
      the base class member is called.
Bisitun answered 29/1, 2011 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.