what's the difference between declaring a virtual function private, protected, and open in the base object?
The difference is that the virtual private function can only be called from the base class. This can be useful if the function is not part of the interface of the external class and is used only by the base class. So users call (some other) members of the base class, and that member calls a virtual function. For instance:
class Base { virtual void stage1()=0;
In addition, there is a point of view that you should not make your virtual functions public at all, because the fact that they are virtual is an inner class and its subclasses, and users should not be aware of this. Rarely, the same function should be overridden and called from external code. This allows the base class to control which (virtual) functions can be called from a (non-virtual) public method, making it easier to maneuver.
See this Herb Sutter article for more details:
... each [publicly available] virtual function performs two tasks: it defines an interface because it is public ...; and it defines the implementation details, namely the internal custom behavior ... That a publicly accessible virtual function inherently has two significantly different jobs - this is a sign that it does not separate the problems, and that we should consider a different approach. What if we want to separate the interface specification from the implementation specification, custom behavior?
...
In general, prefer to make virtual functions of the base class private (or if you really should). This separates interface and implementation issues, which stabilizes interfaces and makes implementation decisions easier to change and reorganize later.
However, I cannot say if this is really widely used ...
source share