user997112
user997112

Reputation: 30635

Initialise data member array elements to zeros on VS2012?

I have a class Y which contains an array of size 100 X objects.

class Y{
    unsigned int x_array[100];
};

I need to initialise this array so that all the elements are zero. Is it this possible to do in Visual Studio and if not what can I do? If I do:

unsigned int x_array[100] = {0}; 

I get a compile error saying data member initialisation is not allowed.

(Intel C++ Compiler v13)

Upvotes: 1

Views: 94

Answers (1)

LihO
LihO

Reputation: 42103

What you are trying to do is available only since C++11, in C++03 the following should do:

class Y{
public:
    Y() : x_array() { }
    unsigned int x_array[100];
};

Also consider using std::vector<unsigned int> instead:

#include <vector>

class Y{
public:
    Y() : x(std::vector<unsigned int>(100, 0)) { }
    std::vector<unsigned int> x;
};

Upvotes: 4

Related Questions