C++/MFC 中的混合搭配类(第 2 部分)
我昨天在谈论重构,而我所采用的方法显然是错误的。我研究了建议的模式,但问题仍然是通用功能实际上是一个超级类的东西,我无法从中派生,因为 MFC 对不同的窗口(CWnd/CDialogEx)提出了自己的要求。
今天我有了一个想法,那就是我可以使用一个超类模板来打包公共逻辑,理论上可以解决问题......
也就是说,我定义模板化的CCommon类,并在所有窗口类中用所需的超类继承它。就像 class CMyWnd : private CCommon
不幸的是,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()
有没有办法可以解决这个问题?
诸如 IMPLMENT_DYNAMIC(template
或 template
似乎根本无法编译。与消息映射相同,我真的很想将其转移到基类中。
I was talking about re-factoring yesterday, and the approach I was going for was obviously wrong. I looked into suggested patterns, but the problem was still that the common functionality is really a thing of a supper class, which I couldn't derive from as the MFC puts on it's own requirements for different windows (CWnd/CDialogEx).
Today I got an idea, that is, I could use a superclass template to pack the common logic which would solve the problem, theoretically...
That is, I define templated CCommon class, and inherit it with required superclass in all window classes. Like class CMyWnd : private CCommon<CWnd>
Unfortunately MFC makes things super ugly because of the macros...
#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()
Is there a way I could work around this problem?
Things like IMPLEMENT_DYNAMIC(template <class T> CCommonWndLogic<T>, T)
or template <class T> IMPLEMENT_DYNAMIC(CCommonWndLogic<T>, T)
doesn't seem to compile at all. Same with message maps, which I would really like carry over to the base class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您正在自找麻烦。 MFC 充满了黑客攻击和遗留的解决方法。除非您可以在源代码级别使用 MFC,否则您的方法并不真正实用。你真的不能。严格使用MFC作为API,按需调用。在您自己的独立类层次结构中完成所有最先进的 C++ 软件工程,只需根据需要调用 MFC 来实现 UI 的详细信息。
You are asking for trouble with this approach unfortunately. MFC is riddled with hacks and legacy workarounds. Your approach isn't really practical unless you can work with MFC at source level. You really can't. Use MFC strictly as an API to be called as required. Do all your state of the art bleeding edge C++ software engineering in your own independent class hierarcy, and just call MFC as required to implement the details of your UI.