Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce the 'override' keyword?

Is there any way to enforce the usage of the C++11 override keyword in Visual C++ 2012?

(i.e. if I forget to say override, then I want to get a warning/error.)

like image 947
user541686 Avatar asked Nov 04 '12 21:11

user541686


People also ask

Is the override keyword required?

You don't need the override identifier to do this in C++, it simply enforces that you are doing it properly.

What is the use of override keyword in C++?

override Keyword in C++ Basically function overriding means redefine a function which is present in the base class, also be defined in the derived class. So the function signatures are the same but the behavior will be different.

What is the role of the override keyword in method declarations?

The name override in the method declaration expresses that the method should override a virtual method of a base class. The compiler checks the assertion. It checks the parameter of the method, the return type of the method, and qualifiers like const and volatile.

What is override in C ++ 11?

C++11 adds two inheritance control keywords: override and final. override ensures that an overriding virtual function declared in a derived class has the same signature as that of the base class. final blocks further derivation of a class and further overriding of a virtual function.


2 Answers

C++11 almost had what you want.

Originally the override keyword was part of a larger proposal (N2928) which also included the ability to enforce its usage:

class A {   virtual void f(); };  class B [[base_check]] : public A {     void f();  // error! };  class C [[base_check]] : public A {   void f [[override]] ();  // OK }; 

The base_check attribute would make it an error to override a virtual function without using the override keyword.

There was also a hiding attribute which says a function hides functions in the base class. If base_check is used and a function hides one from the base class without using hiding it's an error.

But most of the proposal was dropped and only the final and override features were kept, as "identifiers with special meaning" rather than attributes.

like image 192
Jonathan Wakely Avatar answered Oct 06 '22 00:10

Jonathan Wakely


There are few ways to do this in VC++ and equivalent ways with GCC as well.

VC++

Below are the relevant warning numbers in VC++:

C4263 (level 4) 'function': member function does not override any base class virtual member function C4266 (level 4) 'function': no override available for virtual member function from base 'type'; function is hidden 

To enable these two warnings, you can use one of following options:

  1. Set warning level to 4 in project settings and then disable the warnings you don't want. This is my prefered way. To disable unwanted Level 4 warnings, go to project settings > C/C++ > Advanced and then enter warning numbers in Disable Specific Warnings box.
  2. Enable above two warnings using code.

    #pragma warning(default:4263) #pragma warning(default:4266) 
  3. Enable above two warnings in project settings > C/C++ > Command Line and then enter /w34263 /w34266. Here /wNxxxx option means enable xxxx warnings in Level N (N = 3 is default level). You can also do /wdNxxxx which disables the xxxx warning in level N.

GCC

GCC 5.1+ has added new warning suggest-override that you can pass as command line option -Wsuggest-override.

Clang

Clang 3.5+ has -Winconsistent-missing-override, however this only detects cases if some overriding memebers or base classes use override but other overriding members do not. You might want to take a look at clang-tidy tool as well.

like image 30
Shital Shah Avatar answered Oct 06 '22 01:10

Shital Shah