Suppose I have some class Base
:
class Base {
public:
auto foo() -> void { private_foo(); }
auto bar() -> void;
private:
auto private_foo() -> void;
};
There will never be and instance of Base
; it acts only to define an interface and optionally some implementation (here implementation is in foo()
and it calls a private function private_foo()
which it is expecting to be defined by its derived classes). Derived
will be a concrete type and will define the implementations of bar()
and private_foo()
. How can one best achieve this without using virtual inheritance?
My initial thoughts were to define Base
as a class template:
template<typename T>
class Base {
public:
auto foo() -> void { private_foo(); }
virtual auto bar()=0 -> void;
private:
virtual auto private_foo()=0 -> void;
};
Then Derived
can be defined as such:
class Derived: public Base<Derived> {
public:
auto bar() override -> void { // bar implementation };
private:
auto private_foo() override -> void { // private_foo implementation};
};
Is this the best way? Am I correct in assuming that this does not create a virtual function pointer table? Is there a way that is cleaner or more idiomatic? I am aware that I am creating a template class using virtual inheritance, but my impression is that the 'virtual' aspect will be compiled away.
The goal is to create an interface without run-time overhead.