Excerpts from a thread on http://groups.yahoo.com/group/wtl/ around 2003/04/28.
template
class CMyXMLBase : public CExpatImpl <_T>
I can't figure out why everyone keeps doing that. In this case, it's probably a doggerel version of the AbstractTemplate DesignPattern. If so, make CExpatImpl into a concrete class and an abstract interface (IExpat, for example). Then CMyXMLBase inherits IExpat and resolves any methods we need in it, such as OnStartElement or OnEndElement.
Appendix A of ATL Internals talks about this, as well as an article on devx [1]. It's been called "simulated dynamic binding", "ATL style inheritance", "upside down inheritance", "static polymorphism", [the "CuriouslyRecurringTemplate pattern"] and other things. It's used quite a bit in ActiveTemplateLibrary / WindowsTemplateLibrary for various BaseClasses. It's not mutually exclusive to an interface, but is often used in conjunction with an interface (such as IDispatchImpl, IProvideClassInfo2Impl, etc.). It's also the technique used by of all the "MixIn" windowing base classes of ATL / WTL (CWindowImpl, etc.).
Using this approach, in the base class you do something like:
T* pT = static_cast(this);
pT->OverrideableFunction();
It's essentially like having "OverrideableFunction" be a virtual function, but with a couple of benefits:
Item #5 can't be done with virtual classes and functions even if you wanted to. Just one of the many examples of this in the ActiveTemplateLibrary is when you are making a COM object with ATL: you define "BEGIN_COM_MAP", which expands to you defining a handful of static methods and members that the base class CComObjectRootEx calls to help implement QueryInterface.
Now, that's not to say that every single bit of code using "template
-- DanielBowen
[1] <http://archive.devx.com/free/mgznarch/vcdj/1999/julmag99/atlinherit1.asp>
Although the ActivexTemplateLibrary list used to debate whether JimCoplien was the first in print, Coplien himself attributes the [CuriouslyRecurringTemplate] pattern to others. The article Igor mentioned is reproduced in C++ Gems (ISBN 0135705819) -- TimTabor
I must say that the non-traditional inheritance (DynamicBinding) has allowed us to create a really cool skinning engine at our company. We take full advantage of the dynamic binding to create our skinned UI and our lower-level classes. We couldn't have done what we have done in WTL with MicrosoftFoundationClasses or without DynamicBinding. -- ScottAndrew
Gee, why do a lot of porn queries redirect to this topic? :-)