noob_in_math
noob_in_math

Reputation: 61

How to elegantly initialize variables in Class in C++?

class A
{
private:
   int a[100];
public:
   A();
};

Now I want to initialize a with 100 specified value.Except for typing a[i]=XXX in A(), do we have more elegant way?

Upvotes: 0

Views: 48

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595712

Use std::fill(), eg:

#include <algorithm>

A::A() {
    std::fill(a, a+100, DesiredValue);
}

Upvotes: 1

Related Questions