Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to initialize array of boost::scoped_ptr?

I have a class with an array of scoped pointers to objects which do NOT have a default constructor.

The only way I've found to "initialise" them is using swap() like this:

class Bar {
  Bar(char * message) {};
}

class Foo 
{
  boost::scoped_ptr<Bar> arr[2];

  Foo()
  {
    arr[0].swap(boost::scoped_ptr<Bar>( new Bar("ABC") ));
    arr[1].swap(boost::scoped_ptr<Bar>( new Bar("DEF") ));
  };
}

This feels a little verbose and clunky. Have I missed a smarter way of doing it?

like image 988
Roddy Avatar asked Jul 15 '10 16:07

Roddy


1 Answers

arr[0].reset(new Bar("ABC"));
arr[1].reset(new Bar("DEF"));
like image 153
BlueRaja - Danny Pflughoeft Avatar answered Sep 27 '22 21:09

BlueRaja - Danny Pflughoeft