Search code examples
c++interfacelow-latency

How to declare function which must be implemented and not virtual?


I'm using class to declare interface. I just want to define method signature. This method must be implemented in any non-abstract subclass. I don't need method to be virtual. This is default behaviour in C# BTW (i came from C#/Java world)

However it seems in C++ it is not possible. I either declare method in regular way

void Foo::Method()

and then it is not mandatory to implement it or declare method as "pure virtual"

void virtual Foo::Method() = 0;

and then method become virtual, but I want to avoid this to save performance a little bit.

It seems I want to have something like that

void Foo::Method() = 0;

but that would be compilation error


Solution

  • if you're planning on using the derived class from template code, i.e. compile time polymorphism, then you only need to document the expected signature

    the code using a derived class simply won't compile and link if the used function isn't implemented

    otherwise, for runtime polymorphism it needs to be virtual, or else it won't be called