Details - GerdHirsch/Cpp-TemplateFactoryMethod GitHub Wiki

What it demonstrates

There are Template Methods, Hook Methods, pure abstract Hook Methods, i.e. Factory Methods, overloaded HookMethods and Hook Template Methods and even static HookMethods. There is also a non polymorphic NonHookMethod().

They are called in the base-class Implementation in TemplateMethod() and in the derived-class Application in TemplateHookMethod(). It demonstrates the polymorphic use of non virtual operations in different contexts:

  1. context of base-class
  2. context of derived-class

base-class demonstrates

  • how to access the context of the derived-class (This()->) to call polymorphic methods by name shadowing, which is the static polymorphic way to call methods.

The TemplateMethod()demonstrates: call of ( all static / non static)

  • a non polymorphic method via this->
  • a non polymorphic redefined method, don´t do this at home!
  • some polymorphic method via This()->
  • a redefined polymorphic method
  • a redefined overloaded polymorphic method
  • a non redefined polymorphic method (indeed, calls base_type::method)
  • a pure abstract defined polymorphic method
  • a polymorphic template method (TemplateHookMethod)

To redefine a non polymorphic method is never a good idea, regardless of dynamic or static polymorphism. Because the effect is, you send the same message to the same object resulting in different effects, depending on the calling context: in dynamic context Base* or Derived* pointer, in static context using of this or This().

derived-class demonstrates

  • how to give access to protected and private members to the base-class (friend class Implementation<Application>)
  • how to give access to shadowed names of base-class (using base_type::name)

derived-class TemplateHookMethod demonstrates call of (all static / non static)

  • a pure abstract method (cause it´s not declared in base-type)
  • method only defined in base-class
  • method defined in base-class and derived-class (overridden)
  • call of base_type::method of overridden method

Further experiments

Define a member-type in base-class and redefine it in derived-class. Use method of member-type via This()->membertype::operation()