From Gang of Four on the Template Method Pattern:
Three implementation issues are worth noting:
- Using C++ access control. In C++, the primitive operations that a template method calls can be declared protected members. This ensures that they are only called by the template method. Primitive operations that must be overridden are declared pure virtual. The template method itself should not be overridden; therefore you can make the template method a nonvirtual member function.
"This ensures that they are only called by the template method." is not true, is it? As the primitive methods (if some are virtual rather than pure virtual for example) could also be called from a derived class. Is it not true that only declaring the primitive methods private ensures they are only called by the template method? The private virtual primitive methods can then still be implemented (or reimplemented) in subclasses to provide the specialised behaviour which is required within the algorithm defined in the template method in the superclass.
See "Virtuality" from Herb Sutter:
http://www.gotw.ca/publications/mill18.htm
Where he states that:
Guideline #2: Prefer to make virtual functions private. Guideline #3: Only if derived classes need to invoke the base implementation of a virtual function, make the virtual function protected.
I don't see any requirement within the GoF Template Method pattern for derived classes to invoke base class implementations of virtual functions, so why do Gang of Four recommend making these functions protected rather than private?