Newbie: C ++ question about initialization lists

Let's say I have an array of objects declared in my header. The array size can be very large. In my source file, I am creating a constructor for my class and I would like to initialize all the objects in my array. If these objects are not instantiated with the null parameter constructor, I am told to put them in my initialization list.

My question is, do I want to use a loop to initialize this possibly large array of objects that won't end up in the initialization list, right? I would not like to insert my initialization list: str1 ("1"), str2 ("2"), ..., strn ("n"). Can a loop initialize all of these objects in the header, or perhaps in the body of the constructor?

Please let me know. I haven't seen an example of this.

Thanks, JBU

+2


source to share


6 answers


You cannot loop through the initializer list, however, there is no loop problem in the constructor loop as long as the object type has a valid assignment operator. The objects in the array will first be initialized using their null parameter constructor before the constructor body. Then in the body you will redirect them to whatever they need.

Also note that if every object in the array is to be initialized using the same NON-zero parameter constructor, you can use the std :: vector type, and in the initializer list specify the standard non-zero constructor that will be used when allocating the internal array, then there is:



// in .h

class MyClass
{
...
    MyClass();

private:

    vector<SomeObject> objects;
};

// in .cpp

MyClass::MyClass()
: objects(100,SomeObject(10, "somestring"))
{
}

      

+6


source


you will have to wait for C ++ 0x to initialize the array in the initializer list.



struct S { 
    int a[3]; 
    S(int x, int y, int z) :a{x,y,z} { /*…*/ }; // solution to old problem 
};

      

+5


source


It can be done like this using boost to declare the elements const:

#include<vector>
#include<iostream>
#include<boost/foreach.hpp>
#include<boost/assign.hpp>
#include<boost/assign/list_of.hpp>
#include<boost/assign/std/vector.hpp>

using namespace std;
using namespace boost;
using namespace boost::assign;

typedef vector<int> int_vector;

const int_vector my_const_vector = list_of
(1)(2)(3)(5)(8)(13)(21)(34)(55)(89);

class myClass
{
    public :
      // initialization list used in the constructor
      myClass(const int_vector & vec) 
        : m_member_vector(int_vector(vec.begin(), vec.end())) 
      {}
      void print() const 
      { 
           BOOST_FOREACH(int i, m_member_vector) 
           { cout << i << endl; } 
      }
    private :
      int_vector m_member_vector;
};

void main()
{
   myClass m(my_const_vector);
   m.print();
}

      

You can get a promotion from here . I understand that these are not arrays, but this solves your problem - in a way

+4


source


You cannot initialize arrays in the initialization list. You must do this in the body of the constructor.

If the item you want to have in the array is not constructive by default, the simplest solution is to use std :: vector instead of raw array. This way you can create elements by adding them to the body of the constructor.

But if you insist on having a raw array of objects as a member of the class that doesn't have a default constructor, then your only real option is to make it an array of pointers to Foo rather than an array Foo.

+1


source


Not a "newbie" answer, none that I would use, but you can take a look at the boost preprocessor library which allows you to create loops with a preprocessor.

0


source


From your question, it looks like you want to initialize each element of the array with a different parameter (as implied in the list str1 ("1"), str2 ("2"), strn ("n")). This is not possible with current C ++; And as TimW pointed out, C ++ 0X will allow this type of initialization to be supported using sequence constructors.

Having said that, it seems what you want to do is initialize these values ​​("1"), ("2") ... ("n") (do you know these values ​​at compile time?). If so, I don't think you can use an initialization list even in C ++ 0X. If it is not (compiling a time parameter for this very large array) then preprocessor magic is the way to go.

0


source







All Articles