<small id='fizqH'></small><noframes id='fizqH'>

  1. <tfoot id='fizqH'></tfoot><legend id='fizqH'><style id='fizqH'><dir id='fizqH'><q id='fizqH'></q></dir></style></legend>
      <bdo id='fizqH'></bdo><ul id='fizqH'></ul>

  2. <i id='fizqH'><tr id='fizqH'><dt id='fizqH'><q id='fizqH'><span id='fizqH'><b id='fizqH'><form id='fizqH'><ins id='fizqH'></ins><ul id='fizqH'></ul><sub id='fizqH'></sub></form><legend id='fizqH'></legend><bdo id='fizqH'><pre id='fizqH'><center id='fizqH'></center></pre></bdo></b><th id='fizqH'></th></span></q></dt></tr></i><div id='fizqH'><tfoot id='fizqH'></tfoot><dl id='fizqH'><fieldset id='fizqH'></fieldset></dl></div>

      任何人都可以为我提供 C++ 中的单例示例吗?

      时间:2023-10-07
        <bdo id='9AX9F'></bdo><ul id='9AX9F'></ul>

        <legend id='9AX9F'><style id='9AX9F'><dir id='9AX9F'><q id='9AX9F'></q></dir></style></legend>

        <small id='9AX9F'></small><noframes id='9AX9F'>

          • <i id='9AX9F'><tr id='9AX9F'><dt id='9AX9F'><q id='9AX9F'><span id='9AX9F'><b id='9AX9F'><form id='9AX9F'><ins id='9AX9F'></ins><ul id='9AX9F'></ul><sub id='9AX9F'></sub></form><legend id='9AX9F'></legend><bdo id='9AX9F'><pre id='9AX9F'><center id='9AX9F'></center></pre></bdo></b><th id='9AX9F'></th></span></q></dt></tr></i><div id='9AX9F'><tfoot id='9AX9F'></tfoot><dl id='9AX9F'><fieldset id='9AX9F'></fieldset></dl></div>

                  <tbody id='9AX9F'></tbody>

                <tfoot id='9AX9F'></tfoot>
              • 本文介绍了任何人都可以为我提供 C++ 中的单例示例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我用以下方式编写了一个单例 C++:

                I write a singleton c++ in the follow way:

                class A {
                    private:
                        static A* m_pA;
                        A();
                        virtual ~A();
                
                    public:
                        static A* GetInstance();
                        static void FreeInstance();
                
                        void WORK1();
                        void WORK2();
                        void WORK3();
                    }
                }
                
                A* A::GetInstance() {
                    if (m_pA == NULL)
                        m_pA = new A();
                    return m_pA;
                }
                
                A::~A() {
                    FreeInstance()  // Can I write this? are there any potential error?
                }
                
                void A::FreeInstance() {
                    delete m_pA;
                    m_pA = NULL;
                }
                

                谢谢!Evan Teran 和 sep61.myopenid.com 的回答是正确的,而且非常好!我的方法是错误的,我希望任何编写这样代码的人都可以避免我的愚蠢错误.

                Thanks! Evan Teran and sep61.myopenid.com 's answer is right, and really good! My way is wrong, I wish any one writting such code can avoid my silly mistake.

                我的项目中的单例A有一个智能指针向量,另一个线程也可以编辑这个向量,所以当应用程序关闭时,即使我添加了很多CMutex,它也会变得不稳定.多线程错误 + 单例错误浪费了我 1 天时间.

                //-----------------------------------------------------------一个新的单例,如果您认为以下示例有问题,欢迎您

                My singleton A in my project has a vector of smart pointer, and another thread can also edit this vector, so when the application is closing, it always become unstable even I add lots of CMutex. Multithread error + singleton error wasted me 1 day.

                //----------------------------------------------------------- A new singleton, you are welcome to edit if you think there is any problem in the following sample:

                class A {
                    private:
                        static A* m_pA;
                        explicit A();
                        void A(const A& a);
                        void A(A &a);
                        const A& operator=(const A& a);
                        virtual ~A();
                
                    public:
                        static A* GetInstance();
                        static void FreeInstance();
                
                        void WORK1();
                        void WORK2();
                        void WORK3();
                    }
                }
                
                A* A::GetInstance() {
                    if (m_pA == NULL){
                        static A self;
                        m_pA = &self;
                    }
                    return m_pA;
                }
                
                A::~A() {
                }
                

                推荐答案

                您可以使用像这样的静态对象来避免删除它:

                You can avoid needing to delete it by using a static object like this:

                if(m_pA == 0) {
                    static A static_instance;
                    m_pA = &static_instance;
                }
                

                这篇关于任何人都可以为我提供 C++ 中的单例示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:我们能否提高这种面向密钥的访问保护模式的可 下一篇:什么是 C++ 中的代理类

                相关文章

                最新文章

                  <bdo id='GsYZn'></bdo><ul id='GsYZn'></ul>

                  <tfoot id='GsYZn'></tfoot>
                1. <i id='GsYZn'><tr id='GsYZn'><dt id='GsYZn'><q id='GsYZn'><span id='GsYZn'><b id='GsYZn'><form id='GsYZn'><ins id='GsYZn'></ins><ul id='GsYZn'></ul><sub id='GsYZn'></sub></form><legend id='GsYZn'></legend><bdo id='GsYZn'><pre id='GsYZn'><center id='GsYZn'></center></pre></bdo></b><th id='GsYZn'></th></span></q></dt></tr></i><div id='GsYZn'><tfoot id='GsYZn'></tfoot><dl id='GsYZn'><fieldset id='GsYZn'></fieldset></dl></div>
                  <legend id='GsYZn'><style id='GsYZn'><dir id='GsYZn'><q id='GsYZn'></q></dir></style></legend>

                  <small id='GsYZn'></small><noframes id='GsYZn'>