I (hopefully) have understood the purpose of a private virtual function, but I haven't understood why should I have a private pure virtual function! I mean, I have to define this function in all derived classes, but this way all the calls to this virtual pure will be a call to the one defined in the derived.
Suppose we have :
class Base{
public:
void foo(){ bar(); }
private:
virtual void bar() =0 {/*something*/}
};
A derived class of Base must define bar (or have it pure virtual, but forget this for one moment). Because Base::bar is private, it can't be used by the derived class. Because Base is abstract, i can't create Base object, therefore i can call foo only on derived classes, but this means that Base::bar will never be called! Sure, i could have defined foo:
void Base::foo(){
bar();
Base::bar();
}
But for what? It would not be better to define a non virtual private function with the body of bar (and replace Base::bar with it), and make the body of the pure virtual empty?
If so, why should I have bar private (and not protected)?
PS: I have tried to find a solution on internet, and i know there are posts like -> C++: Private virtual functions vs. pure virtual functions , but they help in understanding private virtual function, not private PURE virtual function. If I'm wrong, forgive me!
So in general the pattern is to implement the public non-virtual function and the private or protected virtual function, it looks like you have researched this already, but this is generally known as the Template Method Pattern, with more explanations by Herb Sutter here and here.
Whether to make it pure virtual or not depends on your requirements, sometimes there is absolutely no default behavior that makes sense, or you want to force all the subclasses to implement their own version of this virtual function. Sometimes an empty implementation does make sense. Then provide, but what happens is that other implementors might forget to customize their classes, not implementing the virtual class for which you provided a default behavior. It is really a case by case decision that you will have to make.
There are some corner cases in which you will want to provide an implementation for a pure virtual function, Herb Sutter again, lists two, one to provide default behavior but force derived classes to conciously call, and the other is to protect against compiler issues that might get a pure virtual function called.
There are various concerns comming together at the point where your problem lies, in general make virtual functions private or protected, called by a public function (again Template Method Pattern). If derived classes don't need to call a superclasses implementation, make it private. If you want to implement default behavior make it protected and implement the default behavior in a higher class. If you want to force subclasses to implement their own function (independent of wether there is default behavior or not) make it pure virtual.