c++winapivisual-c++atlwtl

Why does *everything* use a template base class in ATL/WTL?


I'm having a lot of trouble understanding the purpose of templates in ATL/WTL code.

When you look at WTL, you see code like:

template <class TBase>
class CEditT : public TBase
{
    ...
};

typedef CEditT<ATL::CWindow> CEdit;

Why is CEditT defined with a template base class?

In other words, in what scenario would CEditT<T> ever be instantiated where T is not CWindow?


Solution

  • It's so that you can override methods in ATL::CWindow that are called by the ATL/WTL class. If there's something that you don't like in ATL::CWindow, you can derive a class from ATL::CWindow with overridden methods, and then pass along your new class as TBase.

    For example, ATL::CWindow::CenterWindow for a long while had a bug where it did not properly account for multiple monitors, and I've seen people use this technique to override CenterWindow with a fixed implementation.