简体   繁体   English

可以在C ++中制作单例结构吗? 怎么样?

[英]Possible to make a singleton struct in C++? How?

I like to experiment around as I learn more about coding. 随着我对编码的更多了解,我喜欢尝试一下。 I have a program that would only require a single instance of a struct for the life of it's runtime and was wondering if it's possible to create a singleton struct. 我有一个程序,该程序在运行时的生命周期内仅需要一个结构的单个实例,并且想知道是否可以创建一个单例结构。 I see lot's of information on making a singleton class on the internet but nothing on making a singleton struct. 我在互联网上看到很多关于制作单例类的信息,但是关于制作单例结构的信息却很少。 Can this be done? 能做到吗? If so, how? 如果是这样,怎么办?

Thanks in advance. 提前致谢。 Oh, and I work in C++ btw. 哦,我在C ++中工作。

A class and a struct are pretty much the same thing, except for some minor details (such as default access level of their members). classstruct几乎是同一件事,除了一些次要的细节(例如其成员的默认访问级别)。 Thus, for example: 因此,例如:

struct singleton
{
    static singleton& get_instance()
    {
        static singleton instance;
        return instance;
    }

    // The copy constructor is deleted, to prevent client code from creating new
    // instances of this class by copying the instance returned by get_instance()
    singleton(singleton const&) = delete;

    // The move constructor is deleted, to prevent client code from moving from
    // the object returned by get_instance(), which could result in other clients
    // retrieving a reference to an object with unspecified state.
    singleton(singleton&&) = delete;

private:

    // Default-constructor is private, to prevent client code from creating new
    // instances of this class. The only instance shall be retrieved through the
    // get_instance() function.
    singleton() { }

};

int main()
{
    singleton& s = singleton::get_instance();
}

Struct and class are in C++ almost the same (the only difference is default visibility of members). 在C ++中,结构和类几乎相同(唯一的区别是成员的默认可见性)。

Note, that if you want to make a singleton, you have to prevent struct/class users from instantiating, so hiding ctor and copy-ctor is inevitable. 请注意,如果要创建单例,则必须防止struct / class用户实例化,因此隐藏ctor和copy-ctor是不可避免的。

struct Singleton
{
private:
    static Singleton * instance;

    Singleton()
    {
    }

    Singleton(const Singleton & source)
    {
        // Disabling copy-ctor
    }

    Singleton(Singleton && source)
    {
        // Disabling move-ctor
    }

public:
    Singleton * GetInstance()
    {
        if (instance == nullptr)
            instance = new Singleton();

        return instance;
    }
}

Conceptually, a struct and a class are the same in C++, so a making singleton struct is the same as making a singleton class . 从概念上讲, structclass在C ++中是相同的,因此制作单例struct与制作单例class相同。

The only difference between class and struct are the default access specifiers and base class inheritance: private for class and public for struct . classstruct之间的唯一区别是默认的访问说明符和基类继承: class privatestruct public For example, 例如,

class Foo : public Bar 
{ 
 public: 
  int a;
};

is the same as 是相同的

struct Foo : Bar
{
 int a;
};

So, there is no fundamental difference when it comes to singletons. 因此,在单例方面没有根本的区别。 Just make sure to read about why singletons are considered bad . 只要确保阅读一下为什么单身人士被认为是坏人

Here's a simple implementation: 这是一个简单的实现:

struct singleton
{
  static singleton& instance()
  {
    static singleton instance_;
    return instance_;
  }
  singleton(const singleton&)=delete;            // no copy
  singleton& operator=(const singleton&)=delete; // no assignment

 private:
  singleton() { .... } // constructor(s)
};

First off, struct and class only refer to the default access of members. 首先, structclass只引用成员的默认访问权限。 You can do everything with a struct that you can do with a class. 您可以使用可以与类一起执行的结构来完成所有操作。 Now if you were referring to POD structs, things get more complicated. 现在,如果您指的是POD结构,事情将变得更加复杂。 You can't defined a custom constructor, so there's no way to enforce only a single object creation. 您无法定义自定义构造函数,因此无法强制仅创建一个对象。 However, there's nothing stopping you from simply only instantiating it once. 但是,没有什么可以阻止您仅将其实例化一次。

class and struct is almost a synonyms in C++. classstruct 几乎是C ++中的同义词。 For singleton use case they are complete synonyms. 对于单例用例,它们是完整的同义词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM