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. | | | | re: How do I initialize complex class static member?
On Wed, 21 Sep 2005 18:41:44 -0700, Jim Langston wrote:
[color=blue]
> 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;[/color]
class ArrayInitializer
{
public:
ArrayInitializer()
{
// Do your initialization here.
}
};
static ArrayInitializer ai;[color=blue]
> public:
> CParticleStream(void);
> ~CParticleStream(void);
> };
>[/color]
// 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 | | | | re: How do I initialize complex class static member?
"Jay Nabonne" <jay_nabonne@sonicNOSPAM.com> wrote in message
news:pan.2005.09.22.02.08.06.985000@sonicNOSPAM.co m...[color=blue]
> On Wed, 21 Sep 2005 18:41:44 -0700, Jim Langston wrote:
>[color=green]
>> 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;[/color]
>
> class ArrayInitializer
> {
> public:
> ArrayInitializer()
> {
> // Do your initialization here.
> }
> };
> static ArrayInitializer ai;[color=green]
>> public:
>> CParticleStream(void);
>> ~CParticleStream(void);
>> };
>>[/color]
>
> // In the CPP file.
> std::vector<int> CParticleStream::PSArray;
> CParticleStream::ArrayInitializer ai;[/color]
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!!
[color=blue]
> 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.[/color]
Doing that would I still need the line:
InitedVector<int> CParticleStream::PSArray;
to instantize the static variable? | | | | re: How do I initialize complex class static member?
On Wed, 21 Sep 2005 20:45:06 -0700, Jim Langston wrote:
[color=blue]
>[/color]
<snip>[color=blue]
>
> 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;[/color]
Right. What's in the class is a declaration. You still need the
definition of the static in one place.
[color=blue]
>
> 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.[/color]
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);
[color=blue]
>
> Thanks a lot!!
>[color=green]
>> 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.[/color]
>
> Doing that would I still need the line:
> InitedVector<int> CParticleStream::PSArray;
> to instantize the static variable?[/color]
Yes. Otherwise, you'll get an "unresolved external" link error.
- Jay | | | | re: How do I initialize complex class static member?
Jay Nabonne wrote:[color=blue]
> 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.[/color]
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 | | | | re: How do I initialize complex class static member?
"mlimber" <mlimber@gmail.com> wrote in message
news:1127404164.805453.226900@g49g2000cwa.googlegr oups.com...[color=blue]
> Jay Nabonne wrote:[color=green]
>> 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.[/color]
>
> 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 );[/color]
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; |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|