- In C ++ 11, an
override specifier protects against overriding a supposed virtual base function (because the signatures do not match). final specifier protects against inadvertently overriding functions in a derived class.
=> Is there a specifier (something like, perhaps, first or no_override ) that protects against overriding an unknown base function?
I would like to get a compiler error when a virtual function was added to the base class with the same signature as the existing virtual function in the derived class.
EDIT 4 . So that this question is simple and answers the questions, here again
original pseudo code
- abstract
class B : A has private: virtual void fooHasBeenDone() = 0; class C : B implements private: virtual void fooHasBeenDone() override { react(); } private: virtual void fooHasBeenDone() override { react(); }- Now
class A gets a new private: virtual void fooHasBeenDone(); - But the new
A::foo may be different from the original B::foo .
and concrete example
- abstract
class B : A has virtual void showPath() = 0; measure PainterPath class C : B implements virtual void showPath() override { mPath.setVisible(); } virtual void showPath() override { mPath.setVisible(); }- Now
class A gets a new virtual void showPath(); , meaning the path to the file - Now, when A calls showPath (), B shows PainterPath instead of some file path.
Of course, this is wrong, and I have to rename B::showPath() to B::showPainterPath() and implement B::showPath() override . I would just like to get information from the compiler.
Here is a compilation example of the real world:
#include <iostream>
Run it and look at the text output.
For reference, an older example with a specific use case (an instance of PainterPath):
https://ideone.com/6q0cPD (link may have expired)
c ++ inheritance c ++ 11 class-hierarchy modifiers
Martin hennings
source share