2013-03-03 185 views
0

我喜欢在我学习更多关于编码的过程中进行实验。我有一个程序,它只需要一个结构的单个实例来运行它的生命周期,并想知道是否可以创建一个单例结构。我看到很多关于在互联网上创建单例类的信息,但没有提供关于创建单例结构的信息。这可以做到吗?如果是这样,怎么样?可能在C++中创建一个单例结构?怎么样?

在此先感谢。哦,我用C++工作。

+5

记住,C++中的'class'和'struct'是相同的。唯一的区别是'struct'默认成员是'public'。 – 2013-03-03 15:39:18

+0

这是否意味着你可以把方法放在结构中? (如果他们是'一样')。如果是这样,那么我可以做一些像运算符重载在结构中的东西? – Jake 2013-03-03 15:48:36

+1

绝对可以。 – Spook 2013-03-03 15:49:34

回答

3

结构和类在C++中几乎相同(唯一的区别是成员的默认可见性)。

注意,如果你想创建一个单例,你必须防止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; 
    } 
} 
+0

我不知道你可以把方法放在这样的结构中。谢谢。我对此表示赞同,因为一个简单的代码示例是我真正想要的(不是唯一的东西,而是非常重要的东西)之一。 – Jake 2013-03-03 16:21:42

+0

原来这是Java,而不是C++。稍微调整一下就可以使它工作 - 只是它不会像使用g ++那样编译。 – Jake 2013-03-03 19:55:25

6

A classstruct几乎是相同的东西,除了一些小的细节(例如他们的成员的默认访问级别)。因此,例如:

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(); 
} 
+2

@StoryTeller:正确。谢谢你指出。 – 2013-03-03 15:41:04

+2

@Spook:正确。我忘了添加'static'关键字。我只是在脑海里做了这个。编辑,谢谢。 – 2013-03-03 15:42:44

+0

你没有隐藏copy-ctor和=操作符。可以通过复制单例的现有实例来创建实例。如果'singleton'是一个类,就必须像实施单例一样实现单例。 – Spook 2013-03-03 15:48:43

2

从概念上讲,structclass是在C++相同的,所以一个制造单struct相同使得一个单class

classstruct之间的唯一区别是默认访问说明和基类继承:privateclasspublicstruct。例如,

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

相同

struct Foo : Bar 
{ 
int a; 
}; 

所以,没有根本的区别,当谈到单身。只要确保阅读约why singletons are considered bad

这里有一个简单的实现:

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) 
}; 
1

首先,structclass仅指成员的默认访问。你可以用一个你可以用一个类来完成的结构来完成所有任务。现在,如果你指的是POD结构,事情会变得更加复杂。您无法定义自定义构造函数,因此无法强制执行单个对象创建。然而,没有什么能阻止你仅仅实例化一次。

+0

谢谢。非常简洁和全面。读这让我很清楚。 – Jake 2013-03-03 16:23:37

+0

POD的定义:http://stackoverflow.com/questions/146452/what-are-pod-types-in-c – Jake 2013-03-03 16:25:41

+0

现在,等一下,这是否意味着我不能在我的单例结构设计中包含属性?这是关键的一点。程序中结构的一个实例仅用作数据的临时转换位置(具有4个字段的记录)。以下是我为我的结构代码,但希望'添加'代码,使其成为一个单身人士: struct Record { //the record - temporary - reuse - singleton? int DeptNum; char Name[MAXNAME]; int Age; int EmplID; // key field }; Jake 2013-03-03 16:32:12

0

class and struct is almost C++中的同义词。对于单例用例,它们是完全同义词。

相关问题