Skip to main content
gamedev.net
🔒 Locked

Seperate static variables for each class object

Started by Sfpiano Dec 26, 2004 at 8:15 PM 6 replies 450+ views
Original Post
Sfpiano
Sfpiano
I have a class that contains static variables in some of it's functions. Whenever I create multiple instances of this class, the value of the static variable stays the same for each class. Is there a way to make it so that each time I create a new instance of the class it will have it's own static variables?
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Sfpiano
I have a class that contains static variables in some of it's functions. Whenever I create multiple instances of this class, the value of the static variable stays the same for each class. Is there a way to make it so that each time I create a new instance of the class it will have it's own static variables?

Yeah, you don't make them static variables, you make them regular class datamembers :p
JimPrice
JimPrice
Er - maybe I'm missing what you're asking, but if you want each instantiation to have a seperate variable, then surely it's not static? So just leave off the static qualifier?

Jim.
Sfpiano
Sfpiano
-OOP
Thank you for that painfully obvious reply which didn't answer my question. I asked if there was a way for each object to have it's own static variables.

-Jim
Well I have a series of counter variables that are different for each object, but I want them to keep their values throughout the life of the object. I was wondering if there was a way to do this without having a clutter of class variables.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
JimPrice
JimPrice
If you have a counter for multiple instantiations of the same type, then sure a static will work.

If you have a counter per object, then you've got to use a local variable.

If the objects are different (ie there are different clauses for incrementing / decrementing the counter) you could have a FSM with state decided upon in the ctor, to dictate what conditions affect the counter for that object. Arguably this means that they are then different objects, in which case you could be back to statics.

Or I could still be missing the point exactly - can you post some code (or pseudo code) to show what you are trying to do i.e. how do the objects differ?

Jim.
mnansgar
mnansgar
Quote:
Original post by Sfpiano
-OOP
Thank you for that painfully obvious reply which didn't answer my question. I asked if there was a way for each object to have it's own static variables.

-Jim
Well I have a series of counter variables that are different for each object, but I want them to keep their values throughout the life of the object. I was wondering if there was a way to do this without having a clutter of class variables.


A static variable "belongs to the class as a whole, and is, therefore, shared by all objects of the class." (Source)

A local member variable by default has a life equal to that of the object in which it resides. Thus, if I have a "class variable" that is for instance private, then if I alter the variable at any time, it will stay altered until I update its state or deconstruct the object.

Having said that, you mention that you "have a series of counter variables that are different for each object". If I make a static member variable, then that variable will have the same memory address for EVERY instantation. This is clearly not what you want since you want variables that are different for each object. Further, the life of this static variable is equal to the time the program executes.

If we leave out the static qualifier, then we have a normal local class variable. This variable will NOT have the same address for every instantiation; in fact, every time a new object is created, memory will be allocated for these member variables! Thus, since different memory is allocated for every object, then you have variables which "keep their values throughout the life of the object" as you requested! If these counter variables are necessary, then they would not be considered clutter :-) I hope this helps!
h20, member of WFG 0 A.D.
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Sfpiano
-OOP
Thank you for that painfully obvious reply which didn't answer my question. I asked if there was a way for each object to have it's own static variables.

That is the answer. If you want an instance of a variable for every instance of your class, then you quite simply don't want a static variable. If you are asking if there is a way to have it be a non-static member yet keep its scope isolated to a function such that you can only access it from within that function, then, unfortunately, there is no simple way.
Ravyne
Ravyne
There is no such thing as a per-instance static. It is kind of counter-intuitive IMO, one of C++'s shortcomings. I honestly don't know why they didn't introduce another qualifier to resolve all of this, although it all boils down to the so-called "semantic sugar."

As OOP said, you want to use regular class members. Its ugly yes, but it all you've got.

Don't feel too bad about the confusion though, I remember having to go through a bunch of classes my teamates at school had written replacing all the static variables in just about every other method :)
throw table_exception("(? ???)? ? ???");

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.