Constructors and arrays

Hi folks,

Suppose I have something like this:

1
2
3
4
5
class a {
  a(): b(0) { }
  int b;
  int c[2];
};


Is there any way to set both c[0] and c[1] to 0 in the constructor initialiser list (like a(): b(0), c(0,0) { } or something), or must I initialise arrays in the main body?

Cheers.
You can use a std::vector<int> instead of the array. std::vector<T> has a constructor with the signature

explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());
...and is recommended as a substitute for C-style arrays anyways.
Yes, I would usually use a vector, but in this case I specifically need only 2 items, with very basic access (i.e., c[0] and c[1]). It just seems a little overkill to use a vector.
Well, in that case... I don't think you can. I'm not 100% sure though (the actual pointer operation should be illegal).

Edit: looked it up. You can't - an initializer-list consists of ','-seperated initializer-clauses, which are either { initializer-list }s or assignment expressions, which are:
1
2
3
4
assignment-expression:
           conditional-expression
           logical-or-expression assignment-operator assignment-expression
           throw-expression

So de-referencing a pointer is not possible.
Last edited on
Ok, no matter. It is only a minor issue.

Thanks for your help.
Topic archived. No new replies allowed.