私人构造器和make_shared

Private constructor and make_shared

本文关键字:shared make 构造器      更新时间:2023-10-16

我有一个带有私有构造函数的单例类。在静态工厂方法中,我执行以下操作:

shared_ptr<MyClass> MyClass::GetInstance()
{
static once_flag onceFlag;
call_once(onceFlag, []() {
if (_instance == nullptr)
_instance.reset(new MyClass());
});
return _instance;
}

如果我使用

_instance = make_shared<MyClass>();

代码无法编译。我的问题是:为什么new可以调用私有构造函数,make_shared不能?

  1. 如前所述,std::make_shared或其组成部分无权访问私有成员。

  2. call_onceonce_flag是不必要的。它们隐含在 c++11 静态初始化中,

  3. 您通常不希望公开共享指针。

 

class MyClass
{
MyClass() {}
public:
static MyClass& GetInstance()
{
static auto instance = MyClass();
return instance;
}
};

但是,我可以想象一种情况,您希望公开指向 impl 的共享指针 - 这是在类可以选择"中断"或"重置"impl 为新指针的情况下。在这种情况下,我会考虑这样的代码:

class MyClass2
{
MyClass2() {};
static auto& InternalGetInstance()
{
static std::shared_ptr<MyClass2> instance { new MyClass2 };
return instance;
}
public:
static std::shared_ptr<MyClass2> GetInstance()
{
return std::atomic_load(std::addressof(InternalGetInstance()));
}
static void reset() {
std::atomic_store(std::addressof(InternalGetInstance()),
std::shared_ptr<MyClass2>(new MyClass2));
}  
};

但是,最后,我认为类的"静态性"应该是一个实现细节,对类的用户来说并不重要:

#include <memory>
#include <utility>
class MyClass
{
// internal mechanics
struct Impl {
auto doSomething() {
// actual implementation here.
}
};
// getImpl now becomes the customisation point if you wish to change the
// bahviour of the class later
static Impl& getImpl() {
static auto impl = Impl();
return impl;
}

// use value semantics - it makes for more readable and loosely-coupled code
public:
MyClass() {}
// public methods defer to internal implementation
auto doSomething() {
return getImpl().doSomething();
}
};

int main() {
// note: just create objects
auto mc = MyClass();
mc.doSomething();
// now we can pass the singleton as an object. Other functions don't even
// need to know it's a singlton:
extern void somethingElse(MyClass mc);
somethingElse(mc);
}
void somethingElse(MyClass mc)
{
}

请认真对待VTT的评论。


对于您的问题:

我的问题是:为什么new可以调用私有构造函数,make_shared不能?

new实际上在成员函数的 lambda 中使用;好吧,lambda 定义了一个局部类,C++标准允许成员函数中的局部类访问成员函数可以访问的所有内容。记住成员函数可以访问私有函数是微不足道的。

std::make_shared无法访问MyClass的私人。这超出了MyClass的范围


例:

class X{
X(){};
public:
static X* create(){ // This member-function can access private functions and ctors/dtor
auto lm = [](){ // This lambda inherits the same access of enclosing member-function
return new X();
};
return lm();
}
};
int main(){
auto x = X::create();    //valid;
auto y = new X();        //invalid;
}

你应该改用Meyers singleton。您应该确保编译器之前支持 C++11 魔术静态。