Boost logo

Boost Users :

Subject: Re: [Boost-users] How to cast a boost::shared_ptr to void*
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2011-05-10 04:01:05


On Tue, May 10, 2011 at 8:56 AM, Mupparthy Ravindranath
<rv_nath_at_[hidden]>wrote:

> Hi all,
>
> My requirement is to cast an object of type shared_ptr as void*, so that I
> can send it to a callback function that accepts a void*. I cannot change
> callback the function signature because it is part of an existing legacy
> code.
>
> In the existing code, I am sending a raw object pointer (instead of
> shared_ptr). Now, if I want to pass a shared_ptr, it won't compile because
> I cannot cast the shared ptr to void*.
>
> Can anyone suggest some workaround for this. I tried searching the mailing
> lists and internet, but not much luck.
>
>
>
struct X { };
boost::shared_ptr<X> p;
void f( void *);

f( p.get( ) );

should do it. But beware the pointee may be deleted if the last copy of the
shared pointer
goes out of scope, as the raw pointer won't have incremented the reference
count.

- Rob.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net