C++: Constructor Initializer For Arrays

Initializing a member array in constructor initializer

  1. How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible?

Yes. It's using a struct that contains an array. You say you already know about that, but then I don't understand the question. That way, you do initialize an array in the constructor, without assignments in the body. This is what boost::array does.

Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor initializers? Or the invalidness of the above code is a corollary of some other rules?

A mem-initializer uses direct initialization. And the rules of clause 8 forbid this kind of thing. I'm not exactly sure about the following case, but some compilers do allow it.

struct A {
char foo[6];
A():foo("hello") { } /* valid? */
};

See this GCC PR for further details.

Do C++0x initializer lists solve the problem?

Yes, they do. However your syntax is invalid, I think. You have to use braces directly to fire off list initialization

struct A {
int foo[3];
A():foo{1, 2, 3} { }
A():foo({1, 2, 3}) { } /* invalid */
};

C++: constructor initializer for arrays

There is no way. You need a default constructor for array members and it will be called, afterwards, you can do any initialization you want in the constructor.

Initialize an array inside Constructor in C++

You can initialize the array in the constructor member initializer list

A::A() : array{2,3,4,1,6,5,4} {

}

or for older syntax

A::A() : array({2,3,4,1,6,5,4}) {

}

Your sample should compile, using a compiler supporting the latest standard though.


Also note your class declaration is missing a trailing semicolon

class A{
public:
int array[7];
A();
};
// ^

How to initialize std::array member in constructor initialization list when the size of the array is a template parameter

std::index_sequence was provided in order to simplify the metaprogramming task of creating and expanding a pack whose size is not fixed. Use std::make_index_sequence and delegate the construction to some private constructor that deduces the pack:

A(H h) : A(h, std::make_index_sequence<N>{}) {}

template <std::size_t... i>
A(H h, std::index_sequence<i...>) : hashes{((void)i, h)...} {}

Here, ((void)i, h) is a comma expression that evaluates to h, but since we mentioned the pack i inside it, it's eligible to be expanded using .... The result is N copies of h (one for each element of i). This expansion occurs inside a braced-init-list, so the result is a braced-init-list contaning N copies of h, which will then initialize the hashes member.

constructor initialization list for an array in c++

Yes you can. Following is an example. Here you can see it working:

class A
{
int valLen;
int* values;
vector<int> vect;

public:
A(int len, ...)
{
va_list args;
va_start(args, len);
valLen = len;
for(int i=0; i<valLen; i++)
{
vect.push_back(va_arg(args, int));
}
values = &vect[0];
va_end(args);
}

void print()
{
for(int i=0; i<valLen; i++)
cout << values[i]<<endl;
}
};

int main()
{
A aVals[] ={A(3, 50,6,78), A(5, 67,-10,89,32,12)};

for(int i=0; i<2; i++)
{
aVals[i].print();
cout<<"\n\n";
}
return 0;
}

Note: Very first argument of constructor is the number count i.e. number of values to be passed. If count is fixed, then you can skip and make appropriate change in constructor.

Array of unknown length in constructor initializer list

I found the answer to your problem here. So, in your case this solution should be applied like that:

#include <utility>
#include <array>

#define CONSTANT 2

class Data {
public:
Data(int number){}
};

template<typename T, size_t...Ix, typename... Args>
std::array<T, sizeof...(Ix)> repeat(std::index_sequence<Ix...>, Args &&... args) {
return {{((void)Ix, T(args...))...}};
}

template<typename T, size_t N>
class initialized_array: public std::array<T, N> {
public:
template<typename... Args>
initialized_array(Args &&... args)
: std::array<T, N>(repeat<T>(std::make_index_sequence<N>(), std::forward<Args>(args)...)) {}
};

class DemoClass {
private:
initialized_array<Data, CONSTANT> _member;
public:
DemoClass():
_member(1234)
{
// stuff
}
};

Then your _member is statically allocated fixed size array. That approach is a bit complicated though, so maybe someone can provide a cleaner solution.

Initializing member array in constructor initialization list (before C++11)

You may want to take a look as this related question on StackOverflow : using static functions to initialize your member arrays in initialization list can achieve what you are looking for :

class MyClass
{
typedef std::array< int, 2 > t_myA;
static t_myA fillFunction(){ static t_myA const ret = {1,2}; return ret; };

t_myA myArray;

public MyClass();
}

MyClass::MyClass()
: myArray( fillFunction() )
{
}


Related Topics



Leave a reply



Submit