Overrides only for certain derived classes

69 Views Asked by At

Is there a way to make only certain derived classes of a class, call it Base, override a virtual method, call it foo(), of Base? The reason I need this is because all the derived classes of Base that I want to have overrides of Base::foo() I've declared final, and any derived classes of Base henceforth shall derive only from these and call invoke their (final) overrides of foo(). But because Base::foo() is not (and cannot) be declared final, I may accidentally create a new derived class of Base and thus define a catastrophic override of Base::foo(). So how do I prevent this from happening, and force a compiling error if I ever do, apart from making sure that I remember not to?

0

There are 0 best solutions below