views:

366

answers:

2

Hi,

I have just found out that the following is not valid.

//Header File
class test
{
    const static char array[] = { '1', '2', '3' };
};

Where is the best place to initialize this?

Thanks in advance;

+3  A: 
//Header File 
class test 
{ 
    const static char array[];
}; 

// .cpp
const char test::array[] = { '1', '2', '3' }; 
peterchen
Thanks, was not sure if you could do this out side of a member.
No static in the definition, please.
anon
Why are people upvoting code that obviously won't compile?
anon
Because they didn't try to compile it. ^_- Seriously, though, `static`'s many uses confuse a lot of folks.
Mike DeSimone
oops - sorry, That was a copy-and-paste-o, fixed.
peterchen
+11  A: 

The best place would be in a source file

// Header file
class test
{
    const static char array[];
};

// Source file
const char test::array[] = {'1','2','3'};

You can initialise integer types in the class definition like you tried to do; all other types have to be defined and initialised outside the class definition, and only once.

Mike Seymour