开发者

converting boost shared_ptr to void* and vice versa

开发者 https://www.devze.com 2023-03-05 05:25 出处:网络
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 ca

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;

pr开发者_StackOverflow中文版ivate:
};

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?


Just one more asterisk:

boost::shared_ptr<A> b = *(boost::shared_ptr<A>*)(arg);


boost::shared_ptr<A> *b =  ( boost::shared_ptr<A> *)(arg);
0

精彩评论

暂无评论...
验证码 换一张
取 消