Non-default constructor call for an object in vector initialization

I want to initialize a vector in the constructor initialization list. A vector consists of objects with a parameterized constructor. I have:

Class::Class() : 
   raster_(std::vector< std::vector<Cell> > (60, std::vector<Cell>(80)))
{
...

How can I call Cell :: Cell with two parameters in the above line? The obvious:

raster_(std::vector< std::vector<Cell(true,true)> > (60, std::vector<Cell(true,true)>(80)))

does not work.

+3
source share
2 answers

You must try:

Class::Class() : 
     raster_(60, std::vector<Cell>(80, Cell(true, true)))
{
    /* ... */
}

Note that I removed the useless std::vector<std::vector<Cell> >from the initializer. Also keep in mind that this can be very inefficient depending on the cost of copying Cell:

  • std::vector<Cell>, 80 Cell(true, true)
  • std::vector<std::vector<Cell> > 60 ( 80 )!
+2

:raster_(std::vector< std::vector<Cell> > (60, std::vector<Cell>(80, Cell(true, true))));

_ , . _ , , ,

:raster(60, std::vector<Cell>(80, Cell(true, true)))

+1

Source: https://habr.com/ru/post/1776692/


All Articles