473,766 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
~CParticleStrea m(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 4572
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 ArrayInitialize r
{
public:
ArrayInitialize r()
{
// Do your initialization here.
}
};
static ArrayInitialize r ai; public:
CParticleStream (void);
~CParticleStrea m(void);
};


// In the CPP file.
std::vector<int > CParticleStream ::PSArray;
CParticleStream ::ArrayInitiali zer 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*********@so nicNOSPAM.com> wrote in message
news:pa******** *************** *****@sonicNOSP AM.com...
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 ArrayInitialize r
{
public:
ArrayInitialize r()
{
// Do your initialization here.
}
};
static ArrayInitialize r ai;
public:
CParticleStream (void);
~CParticleStrea m(void);
};


// In the CPP file.
std::vector<int > CParticleStream ::PSArray;
CParticleStream ::ArrayInitiali zer 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<in t> 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
"constructo r" 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<in t> 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.goo glegroups.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 PSArrayInitiali zer
{
public:
PSArrayInitiali zer()
{
for ( int i = 0; i < 500; ++i )
PSArray.push_ba ck( i );
}
};
static PSArrayInitiali zer PSAi;
....
};

// Initialize class static variables
std::list<int> CParticleStream ::PSArray;
CParticleStream ::PSArrayInitia lizer CParticleStream ::PSAi;
Sep 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2886
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the int member: MyClass::myStaticMemberInt = 99;
2
5577
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
3
1663
by: Bill Sun | last post by:
Hi, I have a question about to initialize a static map member like this: In the mapclass.h; class mapclass { private: static map<string, int> s_mapArray; }
6
17446
by: markww | last post by:
Hi, I put a static member variable in my class. class CMine { static int m_nCount; }; How do I initialize it to zero? I can't do that in the constructor of the class can I? Won't that keep setting it to zero everytime a new
9
27384
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
4
8368
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I should initialize them in advance. However, the following code, in which I first produce two classses and then try to assign a reference of the first class to a static data member of the second class doesn't work. It gives the following compiler error:
3
2155
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For example, suppose MyStruct has 3 int members. I've tried something like this: class Test {
9
3418
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want to put them in main(), it's so far away. Thanks. -
5
7240
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.