We have a special framework for interfaces in our project, and part of the requirements is that classes which represent an interface may only be used as virtual base classes, not as non-virtual ones. Is there a way to enforce this in code? That is, produce a compilation error if the class is derived from non-virtually.
I have access to C++11 as implemented by VS 2010: this means static_assert
, enable_if
and <type_traits>
are available.
IMO, there is no clean and platform independent solution available to this problem.
The best way is to manually go and change each and every inheritance to virtual
inheritance.
To accomplish that, identifying the derived classes of your interface (say class Base
) is easy(!). Below steps can be followed for that:
class Base
as final
(c++11); i.e. class Base final { ...
virtual
final
keyword and compile the code successfullyThis process (unfortunately) has to be followed periodically, whenever you want to do such sanity checking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With