Initializing a static const member array of a class type

S

Steven T. Hatton

Is there a way to initialize an member array of user defined objects that is
declared static const?

For example:

const vec3f I(1,0,0);
const vec3f j(0,1,0);

class corner_functor
{
public:
corner_functor();

~corner_functor();
static const Vec3f dir[5] = {-I, J, I, -J, -I};//This doesn't work
private:
int index;

};


I also tried some variations of this:

const SbVec3f* corner_functor::dir =
{
-I, J, I, -J, -I
};
 
S

Siemel Naran

Steven T. Hatton said:
Is there a way to initialize an member array of user defined objects that is
declared static const?

For example:

const vec3f I(1,0,0);
const vec3f j(0,1,0);

class corner_functor
{
public:
corner_functor();

~corner_functor();
static const Vec3f dir[5] = {-I, J, I, -J, -I};//This doesn't work
private:
int index;

};

Only const int, long, short, char, bool, and integral constants in general
can be initialized in the class. Try,

class corner_functor
{
...
static const Vec3f dir[5];
};


const Vec3f corner_functor::dir[5] = {-I, J, I, -J, -I};

The above line should occur in the cpp file. You can also define I and J in
the same cpp file.


Note that the following is undefined in ANSI C++

// header.h
extern const Vec3f I;
extern const Vec3f J;

// functor.h
defines class corner_functor;

// functor.cpp
#include "header.h"
const Vec3f corner_functor::dir[5] = {-I, J, I, -J, -I};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top