Reputation: 3086
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++?
Upvotes: 6
Views: 8659
Reputation: 79981
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).
Upvotes: 12
Reputation: 1804
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.
Upvotes: 0
Reputation: 163317
It helps the compiler catch your mistakes two ways:
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.
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.
Upvotes: 2
Reputation: 361582
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.
Upvotes: 0