9

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

1
  • First thing first. Arrays are evil. :) You should try to use vector. No really. And secondly, your static array is guaranteed to be initialized to zero. Dont worry. Commented Aug 26, 2011 at 15:01

4 Answers 4

13

The same as you initialize ordinary arrays:

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

Missing elements will be set to zero.

Sign up to request clarification or add additional context in comments.

7 Comments

:/ Seems like the most appropriate way to do it. It's a large array, an initialization list will be tedious, but it's the most straight forward way to guarantee it's initialized appropriately. Also, other answers that reference the constructor are problematic because they require an instance of the object to be created before the static data members are in a known state.
Perhaps a complex number of values is required that may be necessary to be computed.
Chad: Well, if you must have a static array of 1000 elements, I guess it's your responsibility to specify their initial values :-) Ed: Yes, tough, that's the price you pay. If you had a vector, you could initialize it with an intelligent, value-generating iterator, but since you chose to go for a dumb array, that's a limitation. I think C++11 initializer lists might help, but only if you have an efficient way of constructing the initializer list with your complex values...
I'm still struggling to understand why this has been downvoted -- twice now. Given the OP question of a large c-style array, what is a better way to initialize this properly as a static class variable. I posit that doing so in the constructor is not appropriate (reasons given above).
@Ed: If you were to use C++, you'd use std::array or std::vector -- there's no limit to how much one can fix someone's ideas, but to answer a question one has to stay in scope some times. C++11 adds initializer lists that let you initialize member arrays in a constructor, but as far as this static array is concerned, there's really not much else you can say. Do take it up with the OP if you think his design is wrong, but what has this to do with this answer?
|
3

Try this,

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

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

Comments

1

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

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.