c++ - 基于ATL的CWindowImpl的Direct2D桌面窗口类模板

标签 c++ templates c++11 atl direct2d

Kenny Kerr 在他关于 Direct2D 的教程(part1part2)中展示了如何制作简单的 2D 动画时钟并使用以下类层次结构:

//Desktop window class based on ATL's CWindowImpl
template <typename T> struct DesktopWindow :
CWindowImpl<DesktopWindow<T>, CWindow, CWinTraits<WS_OVERLAPPEDWINDOW | WS_VISIBLE>>
{
    // code here, including:
    void Run() {}
};

//ClockSample class which further extends DesktopWindow
template <typename T> struct ClockSample : T
{
    //code here
};

//SampleWindow class which is empty and is needed, as far as I understand,
//to, so to speak, "untemplate" ClockSample template
struct SampleWindow : ClockSample<DesktopWindow<SampleWindow>>
{
    //empty
};

//main function
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
    SampleWindow window;
    window.Run();
}

我没有包含实际代码,因为它很大而且与问题无关。

struct SampleWindow : ClockSample<DesktopWindow<SampleWindow>> - 我很难理解哪个类继承自哪个类以及这里是什么? SampleWindow 继承自 ClockSample,然后又出现了 SampleWindow,它看起来像是对我的循环引用?如果有人能用简单的语言解释这里到底发生了什么,我会很高兴。

最佳答案

SampleWindow 继承自 ClockSample(“untemplates”),后者又派生自 DesktopWindow,后者又派生自 ATL 的CWindowImpl(进一步将 CWindow 作为基类;CWindowHWND 窗口句柄上的薄包装) .

SampleWindow 作为模板参数允许将代码“向下转换”到后代类并调用重写的方法,而无需将它们设为虚拟。这种方法在 ATL 中被大量使用,尤其是。

例如:

template <typename T> struct DesktopWindow :
CWindowImpl<DesktopWindow<T>, CWindow, CWinTraits<WS_OVERLAPPEDWINDOW | WS_VISIBLE>>
{
    // code here, including:
    void Run()
    {
      T* pT = static_cast<T*>(this); // T = SampleWindow
      pT->InternalRun();
    }
};

struct SampleWindow : ClockSample<DesktopWindow<SampleWindow>>
{
    VOID InternalRun()
    {
      // So we eventually reach here
    }
};

关于c++ - 基于ATL的CWindowImpl的Direct2D桌面窗口类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637710/

相关文章:

c++ - 对自己的互斥锁类使用lock_guard

c++ - 使用模板编译时计算使用 const 值初始化数组

javascript - Simple Polymer 1.0数据表

c++ - 重载 << 运算符以打印 vector 的前 n 个值(不是整个 vector )

c++ - 将二维动态数组传递给 C++ 中的函数

c++ - 在 C++ 中,是否可以定义一个具有不同代码块的函数,以通过另一个函数执行和传递?

c++ - C++ 中带有约束类型的可变参数函数添加

c++ - QScopedpointer 在 Q_DISABLE_COPY 失败

c++ - Qt Creator,Mac OS X : Error: "Symbol(s) not found" when calling function from included header

c++ - 链接器是否识别 namespace 别名?