Static variables in classes

Started by
18 comments, last by Mike737 20 years, 8 months ago
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
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]
------------------------ - Seby -www.dfhi-bomber.fr.st
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
What do you want to do exactly ?

------------------------
- Seby -
www.dfhi-bomber.fr.st
------------------------ - Seby -www.dfhi-bomber.fr.st
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.
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?
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
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;
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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?
// 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;}
How do I set my laser printer on stun?

This topic is closed to new replies.

Advertisement