initialising a vector

N

Nick Keighley

Hi,

is there a way of initialising a vector in a similar fashion to
initialising an array?

consider:
Shape* triangle = new Triangle ();
Shape* square = new Square ();
Shape* shape_arr [] = {&triangle, &square};

is there some of doing something like this?:
vector<Shape*> shape_vec = {&triangle, &square};
 
J

James Daughtry

How about if you combine the two?

Shape* init[] = {new Triangle, new Square};
vector<Shape*> shape_vec(init, init + 2);
 
N

Nick Keighley

[ initialising a vector from an "preset" array ]

James said:
How about if you combine the two?

Shape* init[] = {new Triangle, new Square};
vector<Shape*> shape_vec(init, init + 2);

yes

thanks!
 
J

Jeff Flinn

Nick said:
Hi,

is there a way of initialising a vector in a similar fashion to
initialising an array?

consider:
Shape* triangle = new Triangle ();
Shape* square = new Square ();
Shape* shape_arr [] = {&triangle, &square};

is there some of doing something like this?:
vector<Shape*> shape_vec = {&triangle, &square};

See http://www.boost.org/libs/assign/doc/index.html

which allows you to:

vector<Shape*> shape_vec = List_of(&triangle)(&square};

Jeff Flinn
 
J

Jeff Flinn

Panjandrum said:
And what is the advantage compared to James Daughtry's clean and
simple solution?

Well unless my eyes deceive me, the above is accomplished in a single
statement, which is my understanding as to what the OP desired. Also the
boost assignment library allows use in initializers:

class CompoundShape
{
std::vector<Shape*> mShapes;
public:
CompoundShape()
: mShapes( List_of(new Triangle)(new Square) )
{}
};

Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.

Jeff Flinn
 
K

Kristo

Jeff said:
class CompoundShape
{
std::vector<Shape*> mShapes;
public:
CompoundShape()
: mShapes( List_of(new Triangle)(new Square) )
{}
};

Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.

A smart pointer (one suitable for use in standard containers) will help
you if one new succeeds and the other throws. If either constructor
can throw, it's up to them to free any memory they allocate, per FAQ
17.4.

Kristo
 
F

friedlinguini

Kristo said:
Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.

A smart pointer (one suitable for use in standard containers) will help
you if one new succeeds and the other throws. If either constructor
can throw, it's up to them to free any memory they allocate, per FAQ
17.4.

Just introducing smart pointers does not necessarily solve the leak
problem, though it's a step in the right direction:
http://www.gotw.ca/gotw/056.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top