C ++ Singleton设计模式 [英] C++ Singleton design pattern

查看:84
本文介绍了C ++ Singleton设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我碰到了一个C ++的Singleton设计模式的实现/实现。它看起来像这样(我从现实生活的例子中采用它):

  //很多方法都省略了
class Singleton
{
public:
static Singleton * getInstance();
〜Singleton();
private:
Singleton();
static Singleton * instance;
};



从这个声明我可以推断实例字段是在堆上启动的。这意味着有一个内存分配。对我来说完全不清楚的是什么时候内存将被释放?还是有一个错误和内存泄漏?似乎在实现中有一个问题。



我的主要问题是,如何以正确的方式实现它?


<请参阅这篇文章,了解如何使用保证销毁单例对延迟进行简单设计:

任何人都可以在c ++中为我提供一个Singleton样例? / p>

经典延迟评估并正确销毁单例。

  b $ b {
public:
static S& getInstance()
{
static S instance; //保证被销毁。
//第一次使用时实例化。
return instance;
}
private:
S(){} //构造函数? ({}括号)。

// C ++ 03
// ========
//不要忘记声明这两个。你想确保他们
//是不可接受的,否则你可能会不小心得到
//你的单身人士出现的副本。
S(S const&); // Do not Implement
void operator =(S const&); //不实现

// C ++ 11
// =======
//我们可以使用更好的删除方法的方法
//我们不想要。
public:
S(S const&)= delete;
void operator =(S const&)= delete;

//注意:Scott Meyers在他的Effective Modern
// C ++书中提到,删除的函数通常应该是public的,因为它会产生更好的错误消息
//由于编译器的行为检查无障碍
//删除状态之前
};

请参阅这篇文章,了解何时使用singleton :(不经常)

Singleton:应如何使用



请参阅有关初始化顺序和如何应对的两篇文章:

静态变量初始化顺序

查找C ++静态初始化顺序问题



请参阅这篇文章描述生命周期:

C ++函数中静态变量的生命周期是什么?



请参阅这篇文章,讨论单线程对线程的影响:

将单例实例声明为GetInstance方法的静态变量



请参阅解释为什么双重检查锁定在C ++上无法工作的文章:

C ++程序员应该知道的所有常见的未定义行为是什么?


Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real life example):

// a lot of methods are omitted here
class Singleton
{
   public:
       static Singleton* getInstance( );
       ~Singleton( );
   private:
       Singleton( );
       static Singleton* instance;
};

From this declaration I can deduce that the instance field is initiated on the heap. That means there is a memory allocation. What is completely unclear for me is when exactly the memory is going to be deallocated? Or is there a bug and memory leak? It seems like there is a problem in the implementation.

My main question is, how do I implement it in the right way?

解决方案

See this article for a simple design for a lazy evaluated with guaranteed destruction singleton:
Can any one provide me a sample of Singleton in c++?

The classic lazy evaluated and correctly destroyed singleton.

class S
{
    public:
        static S& getInstance()
        {
            static S    instance; // Guaranteed to be destroyed.
                                  // Instantiated on first use.
            return instance;
        }
    private:
        S() {}                    // Constructor? (the {} brackets) are needed here.

        // C++ 03
        // ========
        // Dont forget to declare these two. You want to make sure they
        // are unacceptable otherwise you may accidentally get copies of
        // your singleton appearing.
        S(S const&);              // Don't Implement
        void operator=(S const&); // Don't implement

        // C++ 11
        // =======
        // We can use the better technique of deleting the methods
        // we don't want.
    public:
        S(S const&)               = delete;
        void operator=(S const&)  = delete;

        // Note: Scott Meyers mentions in his Effective Modern
        //       C++ book, that deleted functions should generally
        //       be public as it results in better error messages
        //       due to the compilers behavior to check accessibility
        //       before deleted status
};

See this article about when to use a singleton: (not often)
Singleton: How should it be used

See this two article about initialization order and how to cope:
Static variables initialisation order
Finding C++ static initialization order problems

See this article describing lifetimes:
What is the lifetime of a static variable in a C++ function?

See this article that discusses some threading implications to singletons:
Singleton instance declared as static variable of GetInstance method

See this article that explains why double checked locking will not work on C++:
What are all the common undefined behaviours that a C++ programmer should know about?

这篇关于C ++ Singleton设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆