c++ - 在 C++/MFC 中混合和匹配类(第 2 部分)

标签 c++ templates mfc

我昨天在谈论重构,而我所采用的方法显然是错误的。我研究了建议的模式,但问题仍然是通用功能实际上是 super 类的东西,我无法从中派生出它,因为 MFC 对不同的窗口 (CWnd/CDialogEx) 提出了自己的要求。

今天我想到了一个想法,就是可以用一个父类(super class)模板来封装通用逻辑,理论上可以解决问题……

也就是说,我定义了模板化的 CCommon 类,并在所有窗口类中用所​​需的父类(super class)继承它。喜欢class CMyWnd : private CCommon<CWnd>

不幸的是,由于宏,MFC 让事情变得非常丑陋......

#pragma once

template <class T> class CCommon : public T
{
    //DECLARE_DYNAMIC(CCommon)

public:
    CCommon();
    virtual ~CCommon();

//protected:
    //DECLARE_MESSAGE_MAP()
};


//IMPLEMENT_DYNAMIC(template <class T> CCommon<T>, CWnd)

template <class T> CCommon<T>::CCommon()
{

}

template <class T> CCommon<T>::~CCommon()
{
}


//BEGIN_MESSAGE_MAP(template <class T> CCommon<T>, CWnd)
//END_MESSAGE_MAP()

有没有办法解决这个问题?

IMPLEMENT_DYNAMIC(template <class T> CCommonWndLogic<T>, T) 这样的东西或 template <class T> IMPLEMENT_DYNAMIC(CCommonWndLogic<T>, T)似乎根本无法编译。与消息映射相同,我真的很想将其转移到基类。

最佳答案

不幸的是,您是在自找麻烦。 MFC 充斥着黑客攻击和遗留变通办法。除非您可以在源代码级别使用 MFC,否则您的方法并不实用。你真的不能。严格使用MFC作为API,按需调用。在您自己的独立类层次结构中完成所有最先进的 C++ 软件工程,并根据需要调用 MFC 来实现您的 UI 的细节。

关于c++ - 在 C++/MFC 中混合和匹配类(第 2 部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4592037/

相关文章:

c++ - 打印 : Wrong bottom margin

c++ - 需要使用递归c++计算算术级数的总和

c++ - 通过 const 成员变量访问内联函数

c++ - 参数列表中的 void_t 有效但不作为返回类型

c++ - 外部函数的模板化类参数

c++ - MFC CView::OnFilePrint 不会打印一份以上

c++ - fftw 拆分示例崩溃

c++ - 使用 'int' 数据类型时出错,即使数字不是小数

c++ - 错误 : no match for 'operator>>' in 'in >> *(arr + ((long unsigned int)(((long unsigned int)i) * 8ul)))'

c++ - 使用 Write 成员将 CString 写入 CFile 时每个字符后为空