A non-virtual interface is an open-participant function that is not virtual, but is usually intended to be implemented in terms of a virtual function that is redefined:
class Interface { public: int compute() { return compute_impl(); } private: virtual int compute_impl() = 0; protected: virtual ~Interface() { } };
The nice thing is that the implementation is actually private , since you can still override private methods — you simply cannot call them from the outside.
In contrast, the abstract interface itself is virtual, and purely in the interface class:
class Interface { public: virtual int compute() = 0; protected: virtual ~Interface() { } };
While both approaches look similar in appearance, the advantage of the inertia of the non-virtual interface is that the interface is not burdened with implementation details of virtual member functions. In other words, the fact that various implementations of an interface satisfies its contract by redefining virtual functions is an implementation detail that is not part of the public aspect of the interface. In particular, the author has the right to change the way the function is implemented in the future, without worrying that users have developed their own inheritance and redefinition hierarchies.
source share