C ++单例模式问题 [英] C++ singleton pattern question

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

问题描述




有人能告诉我这两个单身实物之间有什么区别

实施:


首次实施:


class Singleton

{

public:

static Singleton& instance(){return _instance; }

// ...其他成员

私人:

Singleton();

静态Singleton _instance;

// ...其他会员

};


第二次实施:


class Singleton

{

public:

static Singleton * instance(){return _instance; }

static void Initialize(){_ instance = new Singleton; }

// ...其他会员

私人:

Singleton();

静态Singleton * _instance ;

// ...其他成员

};


我知道在第一次实施中,_instance将被放置在

静态内存中,它将始终存在,而在第二个
实现中,内存只会在堆后分配给

调用Singleton :: Initialize()。但除此之外,两者之间是否有任何实际的区别,比如在什么情况下第一次实施优先于第二种实现(反之亦然)?


谢谢!

Hi

Could someone tell me what''s the difference between these two singleton
implementations:

First implementation:

class Singleton
{
public:
static Singleton& instance() { return _instance; }
//... other members
private:
Singleton();
static Singleton _instance;
//... other members
};

Second implementation:

class Singleton
{
public:
static Singleton* instance() { return _instance; }
static void Initialize() { _instance = new Singleton; }
//... other members
private:
Singleton();
static Singleton* _instance;
//... other members
};

I know that in the first implementation, _instance will be put in the
static memory, and it will always be present, whereas in the second
implementation, memory will only be allocated on the heap after
Singleton::Initialize() is called. But aside from that, is there any
practical difference between the two, like on what circumstances is the
first implementation preferred over the second (or vice versa)?

Thanks!

推荐答案

一方面,指针实现没有清理代码 - 所以单身人士使用的任何

资源可能不会被退回。


问候,


Aiden

< fe **************** @ gmail.com>在消息中写道

news:11 ********************** @ l41g2000cwc.googlegr oups.com ...
For one thing, the pointer implementation has no clean up code - so any
resources the singleton uses may not be returned.

regards,

Aiden
<fe****************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...


有人能告诉我这两个单例实现的区别是什么:

首先实现:

/>类Singleton
{
公开:
静态Singleton& instance(){return _instance; }
// ...其他成员
私人:
Singleton();
静态Singleton _instance;
// ...其他成员
} ;

第二个实现:

类Singleton
公共:
静态Singleton * instance(){return _instance; } static static Initialize(){_ instance = new Singleton; }
// ...其他成员
私人:
Singleton();
静态Singleton * _instance;
// ...其他成员
我知道在第一个实现中,_instance将放在
静态内存中,它将始终存在,而在第二个实现中,内存将只有在调用Singleton :: Initialize()后才在堆上分配。但除此之外,两者之间是否存在实际差异,比如在第二种情况下首选实施首选(反之亦然)?

谢谢!
Hi

Could someone tell me what''s the difference between these two singleton
implementations:

First implementation:

class Singleton
{
public:
static Singleton& instance() { return _instance; }
//... other members
private:
Singleton();
static Singleton _instance;
//... other members
};

Second implementation:

class Singleton
{
public:
static Singleton* instance() { return _instance; }
static void Initialize() { _instance = new Singleton; }
//... other members
private:
Singleton();
static Singleton* _instance;
//... other members
};

I know that in the first implementation, _instance will be put in the
static memory, and it will always be present, whereas in the second
implementation, memory will only be allocated on the heap after
Singleton::Initialize() is called. But aside from that, is there any
practical difference between the two, like on what circumstances is the
first implementation preferred over the second (or vice versa)?

Thanks!




发布通过Usenet.com高级Usenet新闻组服务

------------- ---------------------------------------------

**速度**保留**完成**匿名**

------------------------- ---------------------------------
http://www.usenet.com


是的,但是让我们说我在删除_instance的第二个

实现中添加另一个方法,比如CleanUp(),并且用户需要分别在使用该类之前和之后调用

Initialize和CleanUp 。

第一个和第二个现在功能相同吗?或者有一个

的情况,其中一个实现比另一个更好? (例如在

多线程应用程序中等)?

Yes, but let''s say I add another method, say CleanUp(), in the second
implementation that deletes _instance, and users are required to call
Initialize and CleanUp before and after using the class, respectively.
Are the first and second now functionally identical? Or is there a
situation where one implementation is better than the other? (e.g. in a
multi-threaded application, etc.)?


除了显式与隐式内存管理之外,还有没有真正的

差异 - 就多线程而言,你需要使用

一些同步原语来保护对你的同时访问

单身 - 两种情况都存在同样的情况。


线程的流行问题是第三种可能的实施方式

其中:

MySingleton * instance()

{

静态MySingleton * pInst = new MySingleton();


返回pInst;

}


这会改变您的同步要求 - 但是对于您的特定设计/用途,如何非常依赖

。 br />

希望有所帮助,


问候,

Aiden


< FE **************** @ gmail.com>在消息中写道

news:11 ********************* @ o13g2000cwo.googlegro ups.com ...
Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.

The ''popular'' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();

return pInst;
}

That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.

hope that helps,

regards,
Aiden

<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
是的,但是让我说在删除_instance的第二个
实现中添加另一个方法,比如CleanUp(),用户需要在使用前后调用
Initialize和CleanUp这个类分别是。
第一个和第二个现在功能相同吗?或者是否存在一种实施方式优于另一种实施方式的情况? (例如在
多线程应用程序等中)?
Yes, but let''s say I add another method, say CleanUp(), in the second
implementation that deletes _instance, and users are required to call
Initialize and CleanUp before and after using the class, respectively.
Are the first and second now functionally identical? Or is there a
situation where one implementation is better than the other? (e.g. in a
multi-threaded application, etc.)?




发布通过Usenet.com Premium Usenet新闻组服务

-------------------------------------------------- --------

**速度**保留**完成**匿名**

------------ ----------------------------------------------
http://www.usenet.com

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

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