문제

I have read that if we want to assign a shared_ptr to an already existing address of an object, then we need to define an empty custom deleter so that it is not tried to be deleted from heap. My question is if we have a shared_ptr object allocated using new and another shared_ptr object using an address, will there be any issue freeing the heap memory and maintaining reference counts etc?

some_class a;
shared_ptr<some_class> b(&a, custom_deleter);

shared_ptr<some_class> c(new some_class());
도움이 되었습니까?

해결책

Those shared_ptrs are completely independent. They both manage different objects and will use the appropriate deleter for those objects, so there are no issues with doing that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top