@overrides for c ++?

Is there a way in C ++ so that the virtual method in the subclass actually overlaps the virtual method of the superclass? Sometimes, when I refactor, I forget the method, and then I wonder why it is not called, but I forgot to change the signature of the method so that it no longer overlaps anything.

thanks

+6
source share
2 answers

In C ++ 11, it is possible with the identifier override :

 struct Base { virtual void foo() const { std::cout << "Base::foo!\n"; } }; struct Derived : virtual public Base { virtual void foo() const override {std::cout << "Derived::foo!\n";} }; 

This allows you to find out at compile time if you are unable to override the method. Here we neglect what the const method const :

 struct BadDerived : virtual public Base { virtual void foo() override {std::cout << "BadDerived::foo!\n";} // FAIL! Compiler finds our mistake. }; 
+9
source

This is a C ++ 11 function using the override keyword.

If you are using Visual C ++ 2005 or later, you can use the explicit override function without requiring C ++ 11 support.

For implementation status for different compilers, refer to the Apache stdcxx site .

GCC 4.7.0 implements this function, MSVC implements a standardized version as Visual C ++ 11.0 (will be delivered with the release of Visual Studio 2012).

+1
source

Source: https://habr.com/ru/post/916290/


All Articles