Is there a way to flag (at compile time) "overridden" methods whose signatures do not match the underlying signature?

Basically, I want the C # compiler to execute 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 brings some weird behavior at runtime. I would like my C ++ compiler to catch my poor implementation with something like the C # override . Are there any keywords like "override" implemented in C ++, or do we stick with #define override virtual to show our intent? (in fact, I do not do this - I hate using a preprocessor to "extend" the language).

+7
c ++ override virtual
source share
2 answers

If you can't wait for C ++ 0x, Visual C ++ already has this override keyword. (Since 2005, I reckon). There is the syntax:

 virtual int foo(int) override; 

However, you are not required to enter it. And its non-standard Microsoft extension.

+1
source share

As far as I know, this is not possible in the current standard. You can do this in the upcoming C ++ 0x. See here for more details: Explicit redefinition of virtual functions

+3
source share

All Articles