使用shared_ptr的单例类实现 [英] Singleton class implementation using shared_ptr
问题描述
#include< iostream>
#include< memory>
使用命名空间std;
class Demo {
static shared_ptr< Demo> d;
Demo(){}
public:
static shared_ptr< Demo> getInstance(){
if(!d)
d.reset(new Demo);
return d;
}
〜Demo(){
cout<< 销毁的物体<<恩德尔
}
};
// shared_ptr< Demo>演示:: d(新演示); //接受私有ctor
shared_ptr< Demo>演示:: d;
int main()
{
shared_ptr< Demo> d(Demo :: getInstance());
cout<< d.use_count()<<恩德尔
返回0;
}
- 这是实现单例的正确方法使用shared_ptr
- 的类,请参见上面的注释行,以初始化静态shared_ptr,为什么我们可以在此处创建一个对象,以使用私有构造来初始化shared_ptr
这不是线程安全的:两个调用 getInstance
的线程会导致数据丢失种族。一种常见的方法是使用函数范围的静态变量:
static shared_ptr< Demo> getInstance(){
static shared_ptr< Demo> d(新演示);
return d;
}
这样的变量保证在控制权移交给它时只被初始化一次
尽管如此,目前尚不清楚为什么要使用 shared_ptr
。您也可以这样做
静态演示和放大器; getInstance(){
static Demo d;
return d;
}
这是单例(很好,其中之一)的教科书实现。 / p>
Re:使用私有构造函数初始化。我不确定我是否理解您困惑的本质。您是否在问为什么 Demo :: getInstance
可以使用 Demo
的私有构造函数?好吧,因为它是 Demo
的成员,并且一个班级的成员可以访问该班级的私有成员。您是否在问为什么 Demo :: getInstance
可以调用 shared_ptr< Demo> :: reset()
传递演示*
指针?好吧,因为 reset()
是 shared_ptr
的公共成员函数,所以将指针作为参数。您在此过程的哪一部分有争议?
#include <iostream>
#include <memory>
using namespace std;
class Demo {
static shared_ptr<Demo> d;
Demo(){}
public:
static shared_ptr<Demo> getInstance(){
if(!d)
d.reset(new Demo);
return d;
}
~Demo(){
cout << "Object Destroyed " << endl;
}
};
// shared_ptr<Demo> Demo::d(new Demo); // private ctor is accepted
shared_ptr<Demo> Demo::d;
int main()
{
shared_ptr<Demo> d(Demo::getInstance());
cout << d.use_count() << endl;
return 0;
}
- is this the correct way to implement the singleton class using shared_ptr
- please see above commented line to initialize the static shared_ptr how come we can create an object here to initialize shared_ptr with a private construct
This is not thread-safe: two threads calling getInstance
would cause a data race. A common approach is to use a function-scope static variable:
static shared_ptr<Demo> getInstance(){
static shared_ptr<Demo> d(new Demo);
return d;
}
Such a variable is guaranteed to be initialized exactly once, when control passes over its definition for the first time, and in a thread-safe manner.
At this point though, it's not at all clear why you would want to use shared_ptr
. You could just as well do
static Demo& getInstance(){
static Demo d;
return d;
}
This is a textbook implementation of a singleton (well, one of).
Re: initialize with a private constructor. I'm not sure I understand the nature of your confusion. Are you asking why Demo::getInstance
can use private constructor of Demo
? Well, because it's a member of Demo
, and members of a class can access private members of that class. Are you asking why Demo::getInstance
can call shared_ptr<Demo>::reset()
passing a Demo*
pointer? Well, because reset()
is a public member function of shared_ptr
, taking a pointer as a parameter. Which part of this process do you find controversial?
这篇关于使用shared_ptr的单例类实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!