Search code examples
c++shared-ptrboost-tuples

How to assign a boost::tuple to boost::shared_ptr


In my code I have something like this

shrd_ptr_obj st = boost::make_shared<Myobj>();
Myobj tp =  boost::make_tuple(0,0,0,0,0 );

How do I make st point to tp ?


Solution

  • The natural way is to pass the constructor parameter(s) to make_shared and create the object on the same line.

    shrd_ptr_obj st = boost::make_shared<Myobj>(boost::make_tuple(0,0,0,0,0));
    

    If you want to construct the object in a separate step, you'll need to allocate tp with new rather than creating it on the stack. Then you can create a boost::shared_ptr from this newed pointer.

    Myobj *tp = new Myobj(boost::make_tuple(0,0,0,0,0));
    shrd_ptr_obj st = boost::shared_ptr<Myobj>(tp);