Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to flag (at compile time) "overridden" methods whose signatures don't match base signature?

Basically, I want the C# compiler functionality of its override keyword in my C++ code.

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

class Derived : public Base
{
   virtual int foo(int); // wanted to override Base, but forgot to declare it const
};

As we all know, the above code will compile fine, but yield some strange runtime behavior. I would love my C++ compiler to catch my poor implementation with something like C#'s override keyword. Are there any keywords like "override" being introduced into C++, or are we stuck with #define override virtual to show our intent? (actually, I do not do this - I hate using the preprocessor to "extend" the language).

like image 611
franji1 Avatar asked Oct 08 '10 04:10

franji1


1 Answers

As far I know, this is not possible with the current standard. You can do it in the upcoming C++0x. See here for more details: Explicit virtual function overrides

like image 146
Naveen Avatar answered Oct 13 '22 01:10

Naveen