2

Possible Duplicate:
initialize a const array in a class initializer in C++

If I have a class with a member variable that is an array of bools, how can I initialize the array in the initializer list of the constructor to all false values? Or will they be initialized by default to all false?

class example {
public:
  example()
    : // What goes here to initialize _test to 64 false values?
  {
  }

private:
  bool _test[64];
};
Community
  • 1
  • 1
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • No, by default they aren't initialized to nothing. Can't you iterate over all elements? – DonCallisto Feb 15 '12 at 12:52
  • Why not just a for-loop in the constructor to do it? You could also use memset which is even more efficient. – guitarflow Feb 15 '12 at 12:52
  • [This has already been answered](http://www.google.com/url?q=http://stackoverflow.com/questions/161790/initialize-a-const-array-in-a-class-initializer-in-c&sa=U&ei=B6o7T5eAB8Pu0gGE2cjVCw&ved=0CBcQFjAB&sig2=4ZeSnYECc7vy7ngjYj3YOQ&usg=AFQjCNG7PSUZXNuACyocYKjMa75gOtQg1g) – SplinterOfChaos Feb 15 '12 at 12:51
  • 1
    @guitarflow :Assigning the elements to 0 inside the for-loop (inside the constructor) is not **initialization**. – Prasoon Saurav Feb 15 '12 at 12:59

2 Answers2

2
example() :_test()
    : // zero initialization
  {
  }
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
1

something like:

class example {
public:
  example()
    : _test()
  {  
  }

private:
  bool _test[64];
};
Nim
  • 33,299
  • 2
  • 62
  • 101