void指向boost共享指针向量的指针

时间:2012-09-19 20:07:49

标签: c++ visual-studio

我有一个无效指针

void *vp;

我想在vec_A作为参考传递的函数中指向vec_A,即

some_function(std::vector<boost::shared_ptr<A> >& vec_A)
{
   void *vp;
   //now i want vp to point to vec_A
}

在我将vp指向vec_A后,如何将其恢复为

 std::vector<boost::shared_ptr<A> > 

1 个答案:

答案 0 :(得分:5)

地址和演员:

void * vp = &vec_A;   // address-of

std::vector< boost::shared_ptr<A> > & v =
     *static_cast< std::vector< boost::shared_ptr<A> >* >(vp);

现在v是对同一个向量的引用,该向量的地址在上面的行中。