Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

array initializer for class member

I have a class that contains an array. This array can be constant and static. Is it possible to initialize this array with an array initializer?

For example, if I had a class ThreeSpace to model 3-space, I would want to have three 3-element float arrays for the unit vectors i, j, and k:

class ThreeSpace
{
private:
static const float i[3] = {1, 0, 0};
static const float j[3] = {0, 1, 0};
static const float k[3] = {0, 0, 1};
}

but this code generates an error. Any help would be greatly appreciated!

Josh
[572 byte] By [jab630] at [2007-11-11 6:52:57]
# 1 Re: array initializer for class member
This codfe generates an error because in-class initialization of const static data is possible only with integral types. It isn't possible with arrays either, only sacalar types.
Danny at 2007-11-11 21:02:48 >
# 2 Re: array initializer for class member
BTW, when using static data members, you have to define them outside the class body, which is also an excellent opportunity to initialize them:

class ThreeSpace
{
private:
static float i[3]; //only a declaration
static float j[3]; //ditto

};

///degfinitions + initializations
float ThreeSpace::i[3]={1,0,0};
float ThreeSpace::j[3]={0,1,0};
//...
Danny at 2007-11-11 21:03:53 >