How to cast a boost::shared_ptr to void*

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. regards, RV

On Tue, May 10, 2011 at 8:56 AM, Mupparthy Ravindranath
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.

Hi all, Hi
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.
You can get the raw-pointer using the member-function get() or the free function get_pointer (which should be prefered). smart_ptr<Class> ptr(new Class); Class * raw_ptr = get_pointer(ptr); You can cast the raw-pointer to void* of course. -- Best regards, michi7x7

-------- Original Message --------
Subject: Re: [Boost-users] How to cast a boost::shared_ptr to void*
From: michi7x7
Hi all, Hi
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.
You can get the raw-pointer using the member-function get() or the free function get_pointer (which should be prefered).
Why should the free function be preferred?
smart_ptr<Class> ptr(new Class); Class * raw_ptr = get_pointer(ptr);
You can cast the raw-pointer to void* of course.
participants (4)
-
michi7x7
-
Mupparthy Ravindranath
-
Robert Jones
-
spam-receiver@web.de