How do I call ::std::make_shared on a class with only protected or private constructors?

前端 未结 16 2644
遥遥无期
遥遥无期 2020-11-22 09:07

I have this code that doesn\'t work, but I think the intent is clear:

testmakeshared.cpp

#include 

class A {
 public:
   stat         


        
16条回答
  •  伪装坚强ぢ
    2020-11-22 09:47

    You can use this:

    class CVal
    {
        friend std::shared_ptr;
        friend std::_Ref_count;
    public:
        static shared_ptr create()
        {
            shared_ptr ret_sCVal(new CVal());
            return ret_sCVal;
        }
    
    protected:
        CVal() {};
        ~CVal() {};
    };
    

提交回复
热议问题