So I have a class that uses references (&) with functions like
void request(tcp::socket& socket);
I am starting migrating all my code to boost::shared_ptr<tcp::socket>
but I would really like to know how to turn my shared_ptrs into references so to be capable to make my code evolve function by function and not in one iteration changing all I have into shared_ptr. So how to turn a shared_ptr into a reference?
First, they're not called links, but references. :)
Second, the boost::shared_ptr
can just be dereferenced like normal pointer:
boost::shared_ptr<tcp::socket> p(new tcp::socket());
// can be done in two steps (dereference, pass)
tcp::socket& socket_ref = *p;
request(socket_ref);
// or one step
request(*p);
// ^ -- dereference the shared_ptr
boost::shared_ptr<tcp::socket> sock;
...
if (sock) request( *sock )
I think it's better to rewrite request()
(if you can) so that it can handle boost::shared_ptr
or general pointers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With