2

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
manlio
  • 18,345
  • 14
  • 76
  • 126

4 Answers4

7

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

Salgar
  • 7,687
  • 1
  • 25
  • 39
7

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).

icecrime
  • 74,451
  • 13
  • 99
  • 111
2

boost:shared_ptr overloads operator*:

boost::shared_ptr< T > t_ptr(new T());
*t_ptr; // this expression is a T object

To get a pointer to t you can either use get function or take *t_ptr address:

&*t_ptr; // this expression is a T*

The first method (using get) is probably better, and has less overhead, but it only works with shared_ptrs (or pointers with a compatible API), not with other kind of pointers.

peoro
  • 25,562
  • 20
  • 98
  • 150
  • Technically (although quite unlikely), `operator&()` could be overloaded, so I'd stick with `.get()` – icecrime Nov 10 '10 at 13:23
1

Not dangerous but c-ctor involved.

Client client( *(it->second.get()) );
Valentin H
  • 7,240
  • 12
  • 61
  • 111