Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert boost::shared_ptr to actual class

How would someone do that? for example:

Client* client = it->second;

where it->second is a boost::shared_ptr to Client error:

cannot convert `const ClientPtr' to `Client*' in initialization

2 Answers

boost::shared_ptr has a .get() method to retrieve the raw pointer.

Documentation here about when and why not to use it: http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/shared_ptr.htm

like image 182
Salgar Avatar answered Dec 12 '25 11:12

Salgar


You can use the get method on the boost::shared_ptr to retrieve the pointer, but be very careful in what you do : extracting a naked pointer from a reference counted shared pointer can be dangerous (deletion will be triggered if the reference count reaches zero, thus invalidating your raw pointer).

like image 38
icecrime Avatar answered Dec 12 '25 10:12

icecrime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!