C++/MFC 中的混合搭配类(第 2 部分)

发布于 2024-10-10 02:50:41 字数 1055 浏览 0 评论 0原文

我昨天在谈论重构,而我所采用的方法显然是错误的。我研究了建议的模式,但问题仍然是通用功能实际上是一个超级类的东西,我无法从中派生,因为 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(templateCCommonWndLogic, T)template之类的东西IMPLMENT_DYNAMIC(CCommonWndLogic, T) 似乎根本无法编译。与消息映射相同,我真的很想将其转移到基类中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-10-17 02:50:41

不幸的是,您正在自找麻烦。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文