C ++ Singleton模式 [英] C++ Singleton Pattern

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

问题描述

为什么不是单身人士在C ++中这样做:


A.hpp:


A级

{

public:static A theA;


// ...


private:

A( ){/ * ... * /}

};


A.cpp:


AA: :aA;


而不是复杂的方法我已经看到实例被分配了new?

Why aren''t "Singletons" done like this in C++:

A.hpp:

class A
{
public: static A theA;

// ...

private:
A() { /* ... */ }
};

A.cpp:

A A::theA;

rather than the complicated approaches I''ve
seen with the instance being allocated with "new"?

推荐答案

wk****@yahoo.com 写道:

为什么不是单身人士在C ++中这样做:


A.hpp:


A级

{

public:static A theA;


// ...


private:

A( ){/ * ... * /}

};


A.cpp:


AA: :aA;


而不是复杂的方法我已经看到实例被分配了new?
Why aren''t "Singletons" done like this in C++:

A.hpp:

class A
{
public: static A theA;

// ...

private:
A() { /* ... */ }
};

A.cpp:

A A::theA;

rather than the complicated approaches I''ve
seen with the instance being allocated with "new"?



实际上其中一些是。为什么你说他们不是?


V

-

请删除大写''A'当通过电子邮件回复

我没有回复最热门的回复,请不要问

Actually some of them are. Why do you say they aren''t?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



< a href =mailto:wk **** @ yahoo.com> wk **** @ yahoo.com 写道:

为什么不是单身人士在C ++中这样做:


A.hpp:


A级

{

public:static A theA;


// ...


private:

A( ){/ * ... * /}

};


A.cpp:


AA: :西娅;
Why aren''t "Singletons" done like this in C++:

A.hpp:

class A
{
public: static A theA;

// ...

private:
A() { /* ... */ }
};

A.cpp:

A A::theA;



是什么让我不能复制A的实例?


int main()

{

A b(A :: theA);

}

Whats stopying me from copying an instance of A?

int main()
{
A b(A::theA);
}


>

而不是复杂的方法我已经看到实例被分配了new?
>
rather than the complicated approaches I''ve
seen with the instance being allocated with "new"?



怎么样


#include< iostream>

#include< ostream>


模板< typename T>

T& instance()//仅供参考

{

静态T t;

返回t;

}


A级{

朋友A&实例< A>();

A(){std :: cout<< " A()\\\
英寸; }

A(const A& copy){std :: cout<< 复制A \ n; }

public:

~A(){std :: cout<< "〜A()\\\
英寸; }

};


int main()

{

A& r_a = instance< A>();

// A a = instance< A>(); //错误

std :: cout<< "& r_a =" << & r_a<< std :: endl;

A& r_b = instance< A>();

std :: cout<< & r_b =" << & r_b<< std :: endl;

}


/ *

A()

& r_a = 0x501fe0

& r_b = 0x501fe0

~A()

* /

How about:

#include <iostream>
#include <ostream>

template< typename T >
T& instance() // by reference only
{
static T t;
return t;
}

class A {
friend A& instance< A >();
A() { std::cout << "A()\n"; }
A(const A& copy) { std::cout << "copy A\n"; }
public:
~A() { std::cout << "~A()\n"; }
};

int main()
{
A& r_a = instance< A >();
// A a = instance< A >(); // error
std::cout << "&r_a = " << &r_a << std::endl;
A& r_b = instance< A >();
std::cout << "&r_b = " << &r_b << std::endl;
}

/*
A()
&r_a = 0x501fe0
&r_b = 0x501fe0
~A()
*/

< br>

wk****@yahoo.com aécrit:
wk****@yahoo.com a écrit :

为什么不和单身人士在C ++中这样做:


A.hpp:


A级

{

public:static A theA;

private:

A(){/ * ... * /}

};


A.cpp:


AA :: theA;


而不是复杂的方法我实例被分配为new后,看到

Why aren''t "Singletons" done like this in C++:

A.hpp:

class A
{
public: static A theA;
private:
A() { /* ... */ }
};

A.cpp:

A A::theA;

rather than the complicated approaches I''ve
seen with the instance being allocated with "new"?



你的方法很危险,因为你不知道对象是什么时候创建了b $ b。您输入

main()后才能成功使用它,并且在main()退出后不应该使用它。这意味着你的

单例不能用于静态对象构造等等 - 请注意

这可能最终不会成为问题。


Meyer的单例(getInstance()函数声明一个静态的
变量并返回此变量)可能是你想到的 -

标准确保静态是在你第一次输入函数时构造的,所以它总是有效的 - 直到你退出main()

(静态对象顺序为破坏是明确的,但不容易从程序员的角度来处理,所以你最好不要在退出main()后使用它。
。在

getInstance()函数中使用new()分配实例允许与Meyer的单例相同,但

单例仍然有效,直到内存为止释放。如果它永远不会被释放,那么你的单身人士总是有效的。


*你的方法

A级

{

私人:

静态A theA;

A(){}

~A( ){}

A(const A&){}

A& operator =(const A&);

public:

//程序员不知道构造时间

//破坏时间程序员不知道

A& instance(){return theA; }

};


* Meyer的方法

A级

{

私人:

A(){}

~A(){}

A(const A&){ }

A& operator =(const A&);

public:

//当您输入

//函数时,将创建实例第一次

//破坏时间很难从程序员那里得知

//观点

A& instance(){static A a;返回; }

};


* GoF方法:

A级

{

私人:

A(){}

~A(){}

A(const A&){}

A& operator =(const A&);

public:

//当您输入

//函数时,将创建实例第一次

//实例永远不会被破坏

A& instance(){static A * a = NULL; if(!a)a = new A();返回; }

};


有关单身人士和他们在现代C ++中的可能实现的非常有趣的讨论Andrei Alexandrescu。


问候,


- Emmanuel Deloget,Artware

Your approach is dangerous, as you don''t know when the object is
created. You are only able to successfully use it after you entered in
main(), and you should not use it after main() exit. It means that your
singleton cannot be used in static object construction and so on - note
that this might not be a problem in the end.

Meyer''s singleton (the getInstance() function declares a static
variable and returns this variable) was probably what you got in mind -
the standard ensures that the static is constructed the first time you
entered in the function, so it is always valid - until you quit main()
(static objects order of destruction is well defined, but not easily
handled from the programmer point of view, so you''d better not use it
after you exit main()). Allocating the instance using new() in the
getInstance() function allows the same use as Meyer''s singleton, but
the singleton is still valid until the memory is freed. If it is never
freed then you singleton is always valid.

* Your approach
class A
{
private:
static A theA;
A() { }
~A() { }
A(const A&) { }
A& operator=(const A&);
public:
// time of construction is not known by the programmer
// time of destruction is not known by the programmer
A& instance() { return theA; }
};

* Meyer''s approach
class A
{
private:
A() { }
~A() { }
A(const A&) { }
A& operator=(const A&);
public:
// the instance will be created when you enter the
// function for the first time
// time of destruction is hard to tell from the programmer
// point of view
A& instance() { static A a; return a; }
};

* GoF approach:
class A
{
private:
A() { }
~A() { }
A(const A&) { }
A& operator=(const A&);
public:
// the instance will be created when you enter the
// function for the first time
// the instance is never destroyed
A& instance() { static A *a = NULL; if (!a) a = new A(); return a; }
};

There''s a very interesting discussion about singleton and their
possible implementation in the Modern C++ book of Andrei Alexandrescu.

Regards,

-- Emmanuel Deloget, Artware

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

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