使用指针和使用静态对象的Singleton实现之间的区别 [英] Difference between Singleton implemention using pointer and using static object

查看:152
本文介绍了使用指针和使用静态对象的Singleton实现之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:对不起我的问题不清楚,为什么书/文章喜欢实现#1比实现#2?



使用指针的实际优势是什么实现Singleton类vs使用静态对象?为什么大多数图书都喜欢这个

  class Singleton 
{
private:

static Singleton * p_inst;
Singleton();

public:

static Singleton * instance()
{
if(!p_inst)
{
p_inst = new Singleton();
}

return p_inst;
}
};



  class Singleton 
{
public:
static Singleton& Instance()
{
static Singleton inst;
return inst;
}

protected:
Singleton(); // Prevent construction
Singleton(const Singleton&); //通过复制来防止构造
Singleton& operator =(const Singleton&); //阻止赋值
〜Singleton(); //防止不必要的破坏
};


解决方案


因为大多数描述Singleton反模式的文章并不完全理解所有隐藏的试图在C ++中安全地实现它的危险。


在使用静态对象实现Singleton类vs中使用指针的实际优势是什么? / p>

使用指针,使用 new ,但不要 ,确保对象永远不会被销毁,因此在生命周期结束后,没有访问它的危险。结合懒惰创建,第一次访问,这保证所有的访问都是一个有效的对象。缺点是创建不是线程安全的,并且对象及其获取的任何资源不会在程序结束时释放。



使用本地静态对象,在任何支持C ++ 11线程模型的编译器上,创建都是线程安全的;同样,对象将在程序结束时被销毁。但是,可以在对象被销毁之后访问对象(例如从另一个静态对象的析构函数),这可能导致错误的bug。



最好的选择是避免静态数据和全局可访问的数据。特别是,从不使用Singleton反模式;它结合了全局,静态数据和奇怪的实例化限制,使测试不必要地困难。


EDIT: Sorry my question was not clear, why do books/articles prefer implementation#1 over implementation#2?

What is the actual advantage of using pointer in implementation of Singleton class vs using a static object? Why do most books prefer this

class Singleton
{
  private:

    static Singleton *p_inst;
    Singleton();

  public:

    static Singleton * instance()
    {
      if (!p_inst)
      {
        p_inst = new Singleton();
      }

      return p_inst;
    }
};

over this

class Singleton
{
  public:
    static Singleton& Instance()
    {
        static Singleton inst;
        return inst;
    }

  protected:
    Singleton(); // Prevent construction
    Singleton(const Singleton&); // Prevent construction by copying
    Singleton& operator=(const Singleton&); // Prevent assignment
    ~Singleton(); // Prevent unwanted destruction
};

解决方案

why do books/articles prefer implementation#1 over implementation#2?

Because most articles describing the Singleton anti-pattern don't fully understand all the hidden dangers when trying to implement it safely in C++. It's surprisingly difficult to get it right.

What is the actual advantage of using pointer in implementation of Singleton class vs using a static object?

Using a pointer, with new but no delete, ensures that the object will never be destroyed, so there is no danger of accessing it after its lifetime has ended. Combined with the "lazy" creation, the first time that it's accessed, this guarantees that all accesses are to a valid object. The disadvantages are that creation is not thread-safe, and that the object and any resources it acquires are not released at the end of the program.

Using a local static object, creation is thread-safe on any compiler that supports the C++11 threading model; also, the object will be destroyed at the end of the program. However, it is possible to access the object after its destruction (e.g. from the destructor of another static object), which could lead to nasty bugs.

The best option is to avoid static data, and globally-accessible data, as much as possible. In particular, never use the Singleton anti-pattern; it combines global, static data with weird instantiation restrictions that make testing unnecessarily difficult.

这篇关于使用指针和使用静态对象的Singleton实现之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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