Performing virtual functions that are not publicly available allows the base class to create a protocol around them. If nothing else, this can be used for some accounting / profiling, requiring tools only the base class. The common interface of the base class can be the inline function, which simply forwards the virtual functions. Unfortunately, restrictions can be relaxed in derived classes, i.e. A derived class can give open access to the virtual function from the base class.
In fact, there is another important reason for creating virtual protected functions: when overloading virtual functions (for example, do_put() members in std::num_put<...> ), it is easy to accidentally hide other overloads when overriding only one function. When virtual functions are both a setup point and a call interface, this can easily lead to unexpected behavior. When the functions are virtual protected , it becomes clear that the calling interface and the setting interfaces are actually different, and the problem can be avoided even by using the derived class directly. virtual functions probably want to be protected to allow overriding functions to call the default implementation from the base class. If there is no default implementation, the virtual function can also be private .
This answer discusses why virtual functions should not be public . If you must have virtual functions, first of all, this is a separate question with a somewhat non-trivial answer.
source share