Convert boost shared_ptr to void * and vice versa

Is it possible to convert boost shared_ptr to void* and return to boost::shared_ptr ? I need this because I need to pass a common pointer and a callback function to the timer function implemented in C. And in the callback I need to convert the void* pointer back to boost:shared_ptr . It gave me errors.

I tried the same with a model program.

 #include <iostream> #include <boost/shared_ptr.hpp> using namespace std; class A { public: void f1() { cout<<"function f\n"; } inx aa; private: }; void f2 (void *arg) { boost::shared_ptr<A> b = ( boost::shared_ptr<A> *)(arg); //How to do this } int main () { boost::shared_ptr<A> a (new A()); //void * ptr = (void *) &a; f2( (&a)); return 0; } 

Can this be done with boost::shared_ptr ?

+8
c ++ boost
source share
2 answers

Another star:

 boost::shared_ptr<A> b = *(boost::shared_ptr<A>*)(arg); 
+14
source share
 boost::shared_ptr<A> *b = ( boost::shared_ptr<A> *)(arg); 
+3
source share

All Articles