lundi 13 mars 2017

Initialize vectors size in array of objects

I want to initialize the sizes of vectors in an array of objects.

Every vector have the same size so...

LIB FILE:
#include <vector>
Class NameClass
{
 public:
  explicit NameClass( unsigned size )
  {
    vectorName.resize( size );
  }

  std::vector <type> vectorName;
};

MAIN FILE:

#include "lib.hpp"

int main( void )
{
  NameClass object( size ); #1

  NameClass object[ number_objects ]( size ); #2
  NameClass object[ number_objects ]( NameClass(size), NameClass(size), ... ); #3

  return 0;
}

The #1 works, but is not an array, The #2 doesn't, compiler says "conversion from int to non-scalar type 'NameClass' requested" And #3 works, but... it's just absurd initialize every object. and I can't just put a static to size inside the class, because the value change.

So... My research say that I need use std::generate. the question is... Whats is the best way?

Sorry if is easy question how to use std::generate I'm beginner and get troubles to find the optimal solution.

link std::generate

Aucun commentaire:

Enregistrer un commentaire