1

Inside Myclass.h

Class Myclass
{
public:
Myclass();
private:
static int Myarray[12];
};

How to initialize the above static array ?

0

2 Answers 2

7

You need to define it exactly once, in a .cpp file:

int MyClass::MyArray[12] = { 0, 1, 2 }; /* Definition and initialisation.
                                           Any elements not explicity
                                           initialised will be 
                                           value-initialised,
                                           0 in the case of int. */

The posted code is only a declaration of the array.

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

8 Comments

Now MyArray[0] = 0 , MyArray[1] = 1 , MyArray[2] = 2 ...What is the value going to be stored in rest of the places .
@vivek, see comment in answer.
@hmjd, the rest of the values will be 0.
@SingerOfTheFall, I KNOW! Its in the code comment in my answer.
@hmjd, sorry, I didn't refresh comments before posting.
|
0

Like Viku said: You need to define it exactly once, in a .cpp file:

int MyClass::MyArray[12] = { 0, 1, 2 }; 

and you also need this declare it in your class.h private field private:

static int MyArray[12];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.