469,953 Members | 2,463 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

How do I initialize complex class static member?

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.
Sep 22 '05 #1
5 4282
On Wed, 21 Sep 2005 18:41:44 -0700, Jim Langston wrote:
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
Sep 22 '05 #2

"Jay Nabonne" <ja*********@sonicNOSPAM.com> wrote in message
news:pa****************************@sonicNOSPAM.co m...
On Wed, 21 Sep 2005 18:41:44 -0700, Jim Langston wrote:
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;


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?
Sep 22 '05 #3
On Wed, 21 Sep 2005 20:45:06 -0700, Jim Langston wrote:
<snip>
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!!
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?


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

- Jay
Sep 22 '05 #4
Jay Nabonne wrote:
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

Sep 22 '05 #5
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Jay Nabonne wrote:
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 );


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;
Sep 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by cppaddict | last post: by
2 posts views Thread by Fred Zwarts | last post: by
6 posts views Thread by markww | last post: by
9 posts views Thread by subramanian | last post: by
4 posts views Thread by Bram Kuijper | last post: by
3 posts views Thread by Bob Altman | last post: by
9 posts views Thread by Steven Woody | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.