Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting boost shared_ptr to void* and vice versa

Tags:

c++

boost

Can I convert boost shared_ptr to void* and back to boost::shared_ptr? I need this because I need to pass the shared pointer and a callback function to a timer function implemented in C. And in the call back I need to convert the void* pointer back to boost:shared_ptr. Doing so gave me errors.

I tried the same with a sample 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?

like image 831
punith Avatar asked May 10 '11 07:05

punith


2 Answers

Just one more asterisk:

boost::shared_ptr<A> b = *(boost::shared_ptr<A>*)(arg);
like image 67
Yakov Galka Avatar answered Sep 29 '22 12:09

Yakov Galka


boost::shared_ptr<A> *b =  ( boost::shared_ptr<A> *)(arg);
like image 45
maxim1000 Avatar answered Sep 29 '22 11:09

maxim1000