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
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.
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.
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];
The same as you initialize ordinary arrays:
double MyClass::myarray[1000] = { 1.1, 2.2, 3.3 };
Missing elements will be set to zero.
Try this,
class MyClass {
private :
static double myarray[1000];
};
double MyClass::myarray[]={11,22};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With