混合 MFC 和 STL

发布于 2024-07-10 20:11:30 字数 1449 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(14

枫林﹌晚霞¤ 2024-07-17 20:11:31

我宁愿避免 STL,也不使用它,因为当 MFC 成为事实上的标准大约十年时,它曾经不是那么标准。 而且直到最近版本的 Visual C++(和“标准”STL),MFC 才具有更好的性能。

I prefer to avoid STL and not to use it because it used to be not so standard when MFC was de-facto standard for about a decade. Also until recent versions of Visual C++ (and "standard" STL), MFC just have better performance.

平生欢 2024-07-17 20:11:30

当然。 为什么不?

我使用 MFC 作为表示层,尽管后端的结构和类使用 STL。

Sure. Why not?

I use MFC as the presentation layer, even though the structures and classes in the back-end use STL.

感情废物 2024-07-17 20:11:30

能用STL就用STL,没办法就用MFC

Use STL whenever you can, use MFC when no alternative

辞取 2024-07-17 20:11:30

我总是把它们混合在一起。 唯一的次要 PITA 是序列化 - MFC 容器(CArrayCListCStringArray 等)支持 CArchive序列化,但是当使用 STL 容器时,您必须滚动自己的代码。 最后我转而使用 boost::serialization 并转储了 MFC CArchive 内容。

I mix them all the time. The only minor PITA was serialization - the MFC containers (CArray, CList, CStringArray, etc.) support CArchive serialization, but when using STL containers you have to roll your own code. In the end I switched to using boost::serialization and dumped the MFC CArchive stuff.

李白 2024-07-17 20:11:30

是的,我之前已经混合过它们,没有出现任何问题。 然而,在使用MFC十多年之后,我永远不会考虑将它用于新项目。

Yes, I have mixed them before without problems. However, after using MFC for over a decade, I would never consider using it for a new project.

甜柠檬 2024-07-17 20:11:30

我对所有 C++ 项目都使用 MFC,因为我的项目都不是控制台。 MFC 是 Windows C++ 开发人员的优雅解决方案。 我讨厌 QT,而且我不会在 Windows 上使用 WX。 我不关心可移植性,因为我的应用程序仅适用于 Windows。 我喜欢 MFC/ATL 的 CString 类,std::string 非常原始,没有任何“String”功能。 std::string 只不过是vector

对于所有数据存储和算法,我使用STL。 我也使用 ConcRT PPL 模板类,它与 STL 非常相似。

I use MFC for all my C++ projects, since none of my projects are console. MFC is elegant solution for Windows C++ developers. I hate QT, and I wont use WX on Windows. I dont care about portability, since my applications are only for Windows. I love MFC/ATL's CString class, std::string is very raw, doesn't have any "String" features in it. std::string is nothing more than vector<char>.

For all data storage and algorithms, I use STL. I do also use ConcRT PPL template classes, which are very much same as STL.

温柔女人霸气范 2024-07-17 20:11:30

用于数据层中的集合。 我没有数据支持这一点,但我怀疑模板化的 STL 集合比 MFC 对应的集合性能更高。

For collections in the data layer. I have no data to support this, but my suspicion is that the templated STL collections are more performant than their MFC counterparts.

故事和酒 2024-07-17 20:11:30

是的,我确实混合了它们,因为我发现 MFC 对于正常的自然 C++ 来说太笨拙了。 尽管您可能需要编写一些代码来进行 STL 代码与 MFC 代码对话的转换。

Yes I do mix 'em because I find MFC too unwieldy for normal natural looking c++. Though you might have to write some code for conversions where your STL code talks to MFC code.

青瓷清茶倾城歌 2024-07-17 20:11:30

在 Visual Studio 2003(几乎)完全支持 C++ 标准之前,这是一个非常糟糕的主意。 现在这根本不是一个坏主意。 这是否是个好主意取决于具体情况以及您团队的技能。

It was a very bad idea before Visual Studio 2003's (nearly) full support for the C++ Standard. Now it's not a bad idea at all. Whether it's a good idea depends on the context and what the skillset of your team is.

下壹個目標 2024-07-17 20:11:30

是的,如果满足以下两个条件:

1) 项目选择的语言是 C++(当然,其中包括 STL - STL 中的 S 代表“标准”)。

2) 经过仔细分析,没有找到比 MFC 更好的替代方案或认为它更适合 GUI 支持,因此我的开发团队选择了它。

Yes, if both of the following conditions hold:

1) The language chosen for the project is C++ (which, of course, includes the STL - the S in the STL is for "Standard").

2) After a careful analysis, no better alternative is found or considered appropriate for the GUI support than MFC, and my development team goes for it.

颜漓半夏 2024-07-17 20:11:30

这取决于你对“混合”的定义是什么。
如果您只是想创建一个同时使用 STL 和 MFC 的项目,我认为这没有任何坏处。 它们有不同的目的。

It depends on what your definition of "mixing" is.
If you simply mean creating a project that uses both STL and MFC I don't see any harm in that at all. They serve a different purpose.

梦冥 2024-07-17 20:11:30

将 STL 与其他 Microsoft 标头混合时,请务必定义 NOMINMAX,
否则,您的 std::min 函数将由于 min(a,b) 宏而出现语法错误。

when mixing STL with other microsoft headers, be sure to define NOMINMAX,
otherwise your std::min function will be garbled into a syntax error because of the min(a,b) macro.

风流物 2024-07-17 20:11:30

您不应在 MFC 应用程序中使用标准异常 - 如果将其放入对话框中,您的应用程序可能会挂起。 查看此问题的原因: 为什么我的当我抛出异常时,MFC 应用程序挂起?

You should not use standard exceptions in an MFC application - your app might hang if you throw it inside a dialog. See this question for the reasons: Why does my MFC app hang when I throw an exception?

柠檬心 2024-07-17 20:11:30

我有一个包含许多简单类型字段(UINT、CString、COLORREF 等)的结构。 项目编译顺利。

然后我添加了一个 CArray 到结构中。 它没有编译。

然后我为该结构实现了operator=和复制构造函数(一个使用另一个)。 然后就编译了。

一段时间后,在对结构进行维护时,我做了一个实验:将 CArray 更改为 std::vector 并删除 operator= 和复制构造函数。 它编译得很好,并且在调用operator=或复制构造函数的地方很好地复制了结构。

优点是我可以转储代码中无用的部分——容易出错,并且当有人进行维护并向结构中添加字段时可能不会更新! ——我认为这是一个很大的优势。

原因:

为什么我现在不需要复制构造函数和 = 赋值运算符?

之前,结构体只有简单类型字段。 所以它们是可以复制的。 由于它们全部都是可复制的,因此结构体也可复制。 当我添加 CArray 字段时,这个字段是不可复制的,因为 CArray 派生自 CObject,该类明确地将这两个函数设为私有:

class AFX_NOVTABLE CObject
{
    //...

    private:
        CObject(const CObject& objectSrc);              // no implementation
        void operator=(const CObject& objectSrc);       // no implementation

    //...
}

并且 CArray 作为派生自 CObject 的类,不会执行任何操作来覆盖此行为,因此CArray 将继承它并使其自身不可复制。 在使我的结构可复制之前添加了 CArray 后,我收到了错误:

c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
    c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator ='
    c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(524) : see declaration of 'CObject'
    This diagnostic occurred in the compiler generated function 'CArray<TYPE,ARG_TYPE> &CArray<TYPE,ARG_TYPE>::operator =(const CArray<TYPE,ARG_TYPE> &)'
    with
    [
        TYPE=unsigned int,
        ARG_TYPE=unsigned int &
    ]

The std::vector is copiable by its ownDefinition:

        // TEMPLATE CLASS vector
template<class _Ty,
    class _Ax = allocator<_Ty> >
    class vector
        : public _Vector_val<_Ty, _Ax>
    {   // varying size array of values
public:
    typedef vector<_Ty, _Ax> _Myt;    

注意 _Myt 是向量类本身的 typedef。

    //...

    vector(const _Myt& _Right)
        : _Mybase(_Right._Alval)
        {   // construct by copying _Right
        if (_Buy(_Right.size()))
            _TRY_BEGIN
            this->_Mylast = _Ucopy(_Right.begin(), _Right.end(),
                this->_Myfirst);
            _CATCH_ALL
            _Tidy();
            _RERAISE;
            _CATCH_END
        }

    //...

    vector(_Myt&& _Right)
        : _Mybase(_Right._Alval)
        {   // construct by moving _Right
        _Assign_rv(_STD forward<_Myt>(_Right));
        }

    _Myt& operator=(_Myt&& _Right)
        {   // assign by moving _Right
        _Assign_rv(_STD forward<_Myt>(_Right));
        return (*this);
        }

    //...
 }

因此,向结构/类添加 std::vector 成员字段不需要您在其中实现复制函数,只是因为该新字段。

I had a struct with many simple type fields (UINT, CString, COLORREF, etc.). Project was compiling well.

Then I added a which I added a CArray to the struct. It wasn't compiling.

Then I implemented operator= and copy constructor for that struct (one using the other). It then compiled.

Some time after, doing maintenance to the struct I did an experiment: change the CArray to be a std::vector and remove the operator= and copy constructor. It compiled fine and the struct was being well-copied where operator= or copy constructor were being called.

The advantage is that I could dump a useless part of the code — prone to errors and probably not updated when someone did maintenance adding a field to the struct! — and I see that as a big advantage.

REASON:

Why I don't need the copy-constructor and the = assignment operator now?

Before, the struct had only simple type fields. So they were copiable. Being all of them copiable, makes the struct copiable. When I added the CArray field, this one was no copiable, because CArray derives from CObject, a class that explicilty makes these two functions private:

class AFX_NOVTABLE CObject
{
    //...

    private:
        CObject(const CObject& objectSrc);              // no implementation
        void operator=(const CObject& objectSrc);       // no implementation

    //...
}

And CArray, being a class derived from CObject, doesn't do anything to override this behavior, so CArray will inherit it and rendering itself uncopiable. Having added a CArray before making my struct copiable, I was recieving the error:

c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
    c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator ='
    c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(524) : see declaration of 'CObject'
    This diagnostic occurred in the compiler generated function 'CArray<TYPE,ARG_TYPE> &CArray<TYPE,ARG_TYPE>::operator =(const CArray<TYPE,ARG_TYPE> &)'
    with
    [
        TYPE=unsigned int,
        ARG_TYPE=unsigned int &
    ]

The std::vector is copiable by its own definition:

        // TEMPLATE CLASS vector
template<class _Ty,
    class _Ax = allocator<_Ty> >
    class vector
        : public _Vector_val<_Ty, _Ax>
    {   // varying size array of values
public:
    typedef vector<_Ty, _Ax> _Myt;    

Notice _Myt is a typedef to the vector class itself.

    //...

    vector(const _Myt& _Right)
        : _Mybase(_Right._Alval)
        {   // construct by copying _Right
        if (_Buy(_Right.size()))
            _TRY_BEGIN
            this->_Mylast = _Ucopy(_Right.begin(), _Right.end(),
                this->_Myfirst);
            _CATCH_ALL
            _Tidy();
            _RERAISE;
            _CATCH_END
        }

    //...

    vector(_Myt&& _Right)
        : _Mybase(_Right._Alval)
        {   // construct by moving _Right
        _Assign_rv(_STD forward<_Myt>(_Right));
        }

    _Myt& operator=(_Myt&& _Right)
        {   // assign by moving _Right
        _Assign_rv(_STD forward<_Myt>(_Right));
        return (*this);
        }

    //...
 }

So, adding a std::vector member field to a struct/class will not require you to implement copying functions inside it, only because of that new field.

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