TL; DR : you can, but you shouldn't.
Suppose you want each call to the public interface to be correctly registered (legal requirement for financial services, for example.)
class Engine { public: void SetState( int var, bool val ); { logToFile(); SetStateBool( int var, bool val ); } void SetState( int var, int val ); { logToFile(); SetStateInt( int var, int val ); } private: virtual void SetStateBool(int var, bool val ) = 0; virtual void SetStateInt(int var, int val ) = 0; void logToFile(); };
Since the open interface is not virtual, all derived classes are also automatically registered. If you would SetStateBool make SetStateBool and SetStateInt public, you would not be able to force logging for all derived classes.
Therefore, the recommendation to use the NVI idiom is not a syntactic requirement, but is a tool to provide semantics for the base class (logging or caching) on ββall derived classes.
source share