当构造函数是pretected 或者是private的时候,如何在使用std:make_shared()

@[TOC](当构造函数是pretected 或者是private的时候,如何在使用std:make_shared())

参考链接

https://inneka.com/programming/cpp/how-do-i-call-stdmake_shared-on-a-class-with-only-protected-or-private-constructors/

最简单的方法

class A
{
public:
static std::shared_ptr create()
{
struct make_shared_enabler : public A {};

    return std::make_shared<make_shared_enabler>();
}

private:
A() {}
};

发布了0 篇原创文章 · 获赞 0 · 访问量 85

猜你喜欢

转载自blog.csdn.net/weixin_43866805/article/details/103838878