Skip to main content
added 29 characters in body
Source Link
user2428400
user2428400

Not if you use shared ownership semantics.

Use std::shared_ptr to own the object, if available, else use boost::shared_ptr.

shared_ptr is reference counted. The destructor***SNIP - After clarification of the owned object will not be called until zero shared_ptrs owning that object remainscenario, I posted this answer in existence. Create your object:a comment originally ***

std::shared_ptr<Derived2> B = std::make_shared<Derived2>(); 

ThenHow about this can be copied as much as. In your stack, store unique_ptrs. When you wantpop from the stack, you grab the raw pointer from the unique_ptr and shove it into another unique_ptr - this second one is declared with a custom deleter which, on destruction, retains the actual object B points to will only be deleted once all those copied have gone outlocation of scopethe stack and performs the reverse of the pop operation. This will work with shared_ptr too but obviously it depends on what you need.

AnywayIt's independent of the type of B, go read up about shared_ptrand could be encapsulated entirely in the stack class (barring the unique_ptr+deleter type, there isbut that would just be a lot of cool stuff you can do with it and its buddy unique_ptrtypedef).

Not if you use shared ownership semantics.

Use std::shared_ptr to own the object, if available, else use boost::shared_ptr.

shared_ptr is reference counted. The destructor of the owned object will not be called until zero shared_ptrs owning that object remain in existence. Create your object:

std::shared_ptr<Derived2> B = std::make_shared<Derived2>(); 

Then this can be copied as much as you want, and the actual object B points to will only be deleted once all those copied have gone out of scope.

Anyway, go read up about shared_ptr, there is a lot of cool stuff you can do with it and its buddy unique_ptr.

***SNIP - After clarification of the scenario, I posted this answer in a comment originally ***

How about this. In your stack, store unique_ptrs. When you pop from the stack, you grab the raw pointer from the unique_ptr and shove it into another unique_ptr - this second one is declared with a custom deleter which, on destruction, retains the location of the stack and performs the reverse of the pop operation. This will work with shared_ptr too but obviously it depends on what you need.

It's independent of the type of B, and could be encapsulated entirely in the stack class (barring the unique_ptr+deleter type, but that would just be a typedef).

Source Link
user2428400
user2428400

Not if you use shared ownership semantics.

Use std::shared_ptr to own the object, if available, else use boost::shared_ptr.

shared_ptr is reference counted. The destructor of the owned object will not be called until zero shared_ptrs owning that object remain in existence. Create your object:

std::shared_ptr<Derived2> B = std::make_shared<Derived2>(); 

Then this can be copied as much as you want, and the actual object B points to will only be deleted once all those copied have gone out of scope.

Anyway, go read up about shared_ptr, there is a lot of cool stuff you can do with it and its buddy unique_ptr.