开发者

shared_ptr from pointer

I have a class which can't be create开发者_Python百科d on heap and it has private destructor.

But there is a function which returns a pointer to such constructed object. I want to make a shared pointer from it:

MyClass *GetMyClassPointer() {...}

boost::shared_ptr<MyClass> ptr;
ptr = boost::shared_ptr<MyClass>(GetMyClassPointer()); // [x]

error: ‘MyClass::~MyClass()’ is private

Any ways?


Yes.

It sounds like the instance is being dynamically allocated by a function which has access to the private constructor (either member or friend). Then there should be a public function for cleaning up the instance when you're done with it, which has access to the private destructor (even though you don't).

Use the shared_ptr constructor that accepts a custom deleter, and wire it up to the cleanup function the class provides (may need a wrapper function to get the signature to match).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜