Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : initialize static member large array

Tags:

c++

static

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

like image 780
Vincent Avatar asked Aug 26 '11 13:08

Vincent


People also ask

How do you initialize an entire array with value?

int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index. The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.

Do static variables need to be freed?

Static array or variables will not be freed, when control comes out of that function. Scope of static variable is local to the function in which it is declared, but its lifetime is throughout the program.

How do you initialize an array in C?

Array Initialization Using a Loop The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C. // declare an array. int my_array[5];


2 Answers

The same as you initialize ordinary arrays:

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

Missing elements will be set to zero.

like image 132
Kerrek SB Avatar answered Oct 07 '22 20:10

Kerrek SB


Try this,

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

double MyClass::myarray[]={11,22};
like image 37
KV Prajapati Avatar answered Oct 07 '22 20:10

KV Prajapati