Vincent
Vincent

Reputation: 60491

C++ : initialize static member large array

In order to use static data members in C++, I have currently something like that :

// HEADER FILE .h
class MyClass {
private :
    static double myvariable;
};

// CPP FILE .cpp
double MyClass::myvariable = 0;

But if now I have :

// HEADER FILE .h
class MyClass {
private :
    static double myarray[1000];
};

How can I initialize it ?

Thanks

Upvotes: 9

Views: 4826

Answers (4)

Ed Heal
Ed Heal

Reputation: 60037

Why not do this - change the array to a vector. Use another class that is a superclass of vector and do the initialisation of the array (vector) in its constructor. You are then able to make it as complex as you require and also just treat it as an array>

Upvotes: 1

sehe
sehe

Reputation: 393944

You could add a non-pod static member that would initialize myvariable from it's constructor

This is a little like 'RIAA-by-proxy' if you will.

Beware of the Static Initialization Fiasco

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94653

Try this,

class MyClass {
private :
    static double myarray[1000];
};

double MyClass::myarray[]={11,22};

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 477650

The same as you initialize ordinary arrays:

double MyClass::myarray[1000] = { 1.1, 2.2, 3.3 };

Missing elements will be set to zero.

Upvotes: 13

Related Questions