3

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 ?

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158

1 Answers1

3

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);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578