Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mike737

Static variables in classes

This topic is 5527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have noticed that in C++ when using a class if you use a static variable (encapsulated inside the class), that the same static variable will be shared across all instances of this class. eg. SampleClass Test1; SampleClass Test2; void SampleClass::SampleClass() { static int test; printf("Static addressed @ %p\n",&test); } When using the piece of code above I recieve the output of: Static addressed @ 0045B8CC Static addressed @ 0045B8CC So my problem is how do I get around this? Or is this a bug in C++ (im using the MSVC6 compiler)? Thanks Extracting Patch.... Initializing Windows XP Update Path 2543663B.... Core Dumped, Now Installing Linux..... ---- Mike Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
Advertisement
You can declare a static variable in a constructor, but in the class definition.


class MyClass
{
public :
static int test;
...
};
...
MyClass::MyClass(...)
{
test=5;
...
}


------------------------
- Seby -
www.dfhi-bomber.fr.st

[edited by - theSeby on August 8, 2003 3:23:44 AM]

Share this post


Link to post
Share on other sites
Is there any other possible ways?

I hate doing things such as that because it doesn''t really conform to OO Design. But then again C++ isnt really a full implemented OO language.



Extracting Patch....
Initializing Windows XP Update Path 2543663B....
Core Dumped, Now Installing Linux.....

----
Mike
Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
quote:
Original post by Mike737
I have noticed that in C++ when using a class if you use a static variable (encapsulated inside the class), that the same static variable will be shared across all instances of this class.


Er... this is exactly what static inside a class is supposed to do.

Share this post


Link to post
Share on other sites
How exactly do you want to get around this? You use a static variable for the exact reason you mentioned, to share an a variable across all instances of a class. Are you actually looking for a solution whereby each instance of a class is aware of the other one in someway?

Share this post


Link to post
Share on other sites
quote:
Original post by BitMaster
Er... this is exactly what static inside a class is supposed to do.


Oh?! When using VB static variables are actually not shared... they are relative to that particular instance of that class so SampleClass1 = 1 while SampleClass2 = 2.


I''m using static variables for a number of functions, but the main concerning one is that I have several functions inside my class which keep the last time that that particular function was called. Then I check whether enough time has elapsed for me to do an update on that function. There are about 100 instances of the 1 particular class running as it is my bot class.

So I guessing that I''m gonna have to bite the bullet and put them in the class declaration?

Extracting Patch....
Initializing Windows XP Update Path 2543663B....
Core Dumped, Now Installing Linux.....

----
Mike
Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
I think you are looking for something like this:


#include <iostream>

class foo
{
public:
foo(): my_id(++count)
{
std::cout << "I am instance " << my_id << "." << std::endl;
}
int my_id;
static int count;
}
foo::count = 0;

foo foo1, foo2, foo3;

Share this post


Link to post
Share on other sites
it''s not a bug, a static variable sticks with what it is supposed to. if you want a specific variable for a class what''s wrong with members?

Share this post


Link to post
Share on other sites

// MyClass.h File:

class MyClass
{
static long test;
};

// MyClass.cpp File:

long MyClass::test =0;



Maybe you mean this:

class MyClass
{
void function (void);
}

void MyClass:function(void)
{
static access=0;

std::cout << "I have been accessed " << ++access << " times";
}

Now the static is not shared between the instances. Access will be set to 0 when function is called for the first time, after that this line is ignored (This is only valid for variable=0 I think).

You don''t need to use static for that in any case though, that''s the nice thing about a class.

class MyClass
{
long functionCounter;

void function (void);
}

void MyClass::function(void)
{
++functionCounter;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!