0

I was working with singleton pattern and shared_ptr.I was trying to make the code like this:

class A{
    private:
        static std::shared_ptr<A> instance;
        A();
    public:
        static std::shared_ptr<A> creatInstance();
};

std::shared_ptr<A> A::creatInstance(){
    if(!instance){
        instance=std::make_shared<A>();
    }
    return instance;
}

But I got a compiler error. Any thoughts? I tried to make make_shared be a friend function of class A,but it didn't work.

friend shared_ptr<A> make_shared<A>();

1 Answers1

0

It seems that std::make_shared< A > requires public constructor of A.

Here is a few solutions of this problem - How do I call ::std::make_shared on a class with only protected or private constructors?

Community
  • 1
  • 1
Robert Wadowski
  • 1,407
  • 1
  • 11
  • 14