How do I initialize complex class static member?

J

Jim Langston

What I want to do: have a vector of ints in my class initialized with 0 to
499 which will later be pushed/popped out of the vector by instances.

What I have:

class CParticleStream // Yes, I know you wouldn't use 'C'
{
private:
static std::vector<int> PSArray;
public:
CParticleStream(void);
~CParticleStream(void);
};

Now, my understanding is if it was a simle variable, such as an int I would
only have to do:
CParticleStream::MyInt = 0;

Since this is a vector and I need a for loop I can't do that outside a
function body.

I'm looking at having to create *another* variable, such as a bool
initialized, initialize it to false, then in my constructor if initialized
is false load my vector and set initialized to true.

Is there some other way to do this without having to use a second variable?
I could also make it a public method and initialize it at the top of main,
but only this class will be using this vector.

I can't simply look for a size of 0 becuase this vector can shrink in it's
lifetime and may shrink down to 0 (and it does not need to be initalized
then).

Incidently, what is a static class member called? Simply a static class
member?

------------------------------------------------

Why I actually need this: I'm using a DirectX engine that has "particle
streams" that you reference via an int index, 0 to 499. Internally to the
engine there is an array of 500 elements that this index references.
Particle streams will come and go and I need some method of keeping track of
which ones are used and which ones are available.

If someone could think of a better way to do this I'm all ears.

Thanks.
 
J

Jay Nabonne

What I want to do: have a vector of ints in my class initialized with 0 to
499 which will later be pushed/popped out of the vector by instances.

What I have:

class CParticleStream // Yes, I know you wouldn't use 'C'
{
private:
static std::vector<int> PSArray;

class ArrayInitializer
{
public:
ArrayInitializer()
{
// Do your initialization here.
}
};
static ArrayInitializer ai;
public:
CParticleStream(void);
~CParticleStream(void);
};

// In the CPP file.
std::vector<int> CParticleStream::pSArray;
CParticleStream::ArrayInitializer ai;

You can even provide arguments to the "ai" constructor if you like.

Alternatively, you could inherit an array class from std::vector<int>,
give it a constructor that does the initialization and then use that
object in your class.

- Jay
 
J

Jim Langston

Jay Nabonne said:
class ArrayInitializer
{
public:
ArrayInitializer()
{
// Do your initialization here.
}
};
static ArrayInitializer ai;

// In the CPP file.
std::vector<int> CParticleStream::pSArray;
CParticleStream::ArrayInitializer ai;

Very elegent. This also shows me that I don't completely understand
static class members. This would seem to me to indicate that if it
was an int, instead of a vector the correct initialization syntax
would be:
int CParticleStream::MyInt = 0;
and not
CParticleStream::MyInt = 0;

So this seems to indicate that I am actually instataizing the vector
at this point, not just initializing it. So it in fact becomes a
global variable. Hmm...I need to STFW on static class members.

Thanks a lot!!
Alternatively, you could inherit an array class from std::vector<int>,
give it a constructor that does the initialization and then use that
object in your class.

Doing that would I still need the line:
InitedVector<int> CParticleStream::pSArray;
to instantize the static variable?
 
J

Jay Nabonne

This also shows me that I don't completely understand
static class members. This would seem to me to indicate that if it
was an int, instead of a vector the correct initialization syntax
would be:
int CParticleStream::MyInt = 0;
and not
CParticleStream::MyInt = 0;

Right. What's in the class is a declaration. You still need the
definition of the static in one place.
So this seems to indicate that I am actually instataizing the vector
at this point, not just initializing it. So it in fact becomes a
global variable. Hmm...I need to STFW on static class members.

That's right. The vector gets default constructed at that point. It's
actually quite cool that you can even pass constructor arguments at that
point.

For instance, your above int line could also be written (using
"constructor" notation):

int CParticleStream::MyInt(0);
Thanks a lot!!


Doing that would I still need the line:
InitedVector<int> CParticleStream::pSArray;
to instantize the static variable?

Yes. Otherwise, you'll get an "unresolved external" link error.

- Jay
 
M

mlimber

Jay said:
Alternatively, you could inherit an array class from std::vector<int>,
give it a constructor that does the initialization and then use that
object in your class.

If it's just default initialization, you could use vector's existing
constructors:

// Allocate 500 values and initialize all to 10
vector<int> CParticleStream::pSArray( 500, 10 );

or

// Initialize all values to int(), i.e., 0
vector<int> CParticleStream::pSArray( 500 );

Cheers --M
 
J

Jim Langston

mlimber said:
If it's just default initialization, you could use vector's existing
constructors:

// Allocate 500 values and initialize all to 10
vector<int> CParticleStream::pSArray( 500, 10 );

or

// Initialize all values to int(), i.e., 0
vector<int> CParticleStream::pSArray( 500 );

I know this, but unfortunately I need to initialize 0 through 499, not all
to the same value.

I used the class method of initializing it and it seems to work fine.

class CParticleStream
{
private:
// Static vector of available particle stream indexes
static std::list<int> PSArray;
class PSArrayInitializer
{
public:
PSArrayInitializer()
{
for ( int i = 0; i < 500; ++i )
PSArray.push_back( i );
}
};
static PSArrayInitializer PSAi;
....
};

// Initialize class static variables
std::list<int> CParticleStream::pSArray;
CParticleStream::pSArrayInitializer CParticleStream::pSAi;
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top