Static Variables

Started by
9 comments, last by D_JildQuantum 20 years, 9 months ago
I have been teaching myselfVisual C++ for 2 years, and i usually dont post here because i dont need to, but i just realized, i never came across what a static variable actually is... i was looking at a friends code, and saw a static variable, but i cant ask him becuase he wont be on for 2 months as he has gone on vacation. here is his example:

class CApplication
{
//other stuff here

public:
//more stuff
static int Execute(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
}
 
sorry if its a stupid queston -.-
Quantum
Advertisement
Static vars are the same for all instances of your class, so if you have 20 instances of this class:
class CSomeClass{public:// Some stuff hereprivate:static int theBestestIntEvar;}; 

there would still only be one theBestestIntEvar. From your member functions, you access it just as if it was a normal member, but from outside of your class, you access it like:
CSomeClass::theBestestIntEvar = 1337;

"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."
That is true but in his case Execute is a static member function which basicly means it a regular function but is in CApplication''s namespace and can be inherited. It also means it can be accessed without an instance of CApplication and can''t use any of CApplication''s member variables.
ASCII stupid question, get a stupid ANSI
quote:Original post by Yohumbus
That is true but in his case Execute is a static member function which basicly means it a regular function but is in CApplication''s namespace and can be inherited. It also means it can be accessed without an instance of CApplication and can''t use any of CApplication''s member variables.


Well, they can only use CApplications static member variables (it makes perfect sense of course).

OP: I''m actually kind of surprised that you haven''t seen static variables after 2 years of experience (no offense or anything).

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Ya i know, sorry about the questions... most of that two years was learning VB anyway...
Quantum
A staic variable is one that is only executed once.

Scott Simontis
C++ Guy
Scott SimontisMy political blog
quote:Original post by D_JildQuantum
I have been teaching myselfVisual C++ for 2 years, and i usually dont post here because i dont need to, but i just realized, i never came across what a static variable actually is... i was looking at a friends code, and saw a static variable, but i cant ask him becuase he wont be on for 2 months as he has gone on vacation. here is his example:
class CApplication{//other stuff herepublic://more stuffstatic int Execute(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);}  


sorry if its a stupid queston -.-


in case no one noticed, D_J has a static member function ...
so maybe someone wants to tell him how that works also.

Beginner in Game Development?  Read here. And read here.

 

People here have given decent definitions of static member variables and functions, but here''s a concrete example. This is how you would use a static member variable to count the number of instances of a class that exist at some point.

struct SomeClass{  SomeClass() { ++instanceCount; }  ~SomeClass() { --instanceCount; }  static int instanceCount;};// In C++ you need to initialize static member// variables somewhere outside the class definition.// You can''t do it in a constructor like a normal member// variable (why? think about it a little while).SomeClass::instanceCount = 0;


To access instanceCount inside a member function of SomeClass you just call it instanceCount. Outside SomeClass it''s called SomeClass::instanceCount.

Now naturally you don''t want just anyone to be able to modify instanceCount since it should only change when the SomeClass constructor or destructor is called. You should make it private. If it''s private, how do you access it? You can''t make a normal getInstanceCount function like you would for a normal private variable. You make a static member function:

struct SomeClass{  SomeClass() { ++instanceCount; }  ~SomeClass() { --instanceCount; }  static int getInstanceCount() { return instanceCount; }private:  static int instanceCount;};SomeClass::instanceCount = 0;


Static member functions are accessed in the same way as static member variables. Inside SomeClass it would just be getInstanceCount(), but outside SomeClass it becomes SomeClass::getInstanceCount()

Clear?
OH!!!!!!!!! i see! so from what i understand Static variables are:

Variables that are only created once, and any time you change it, it changes throughout the whole function, sorta like global variables in a class,

and Static Functiosn are functions used inside a class to work with static variables, because thats the nly thing from within the function they can work with, and other functions that arnt static can only access variables that arnt static?

Quantum
Quantum
Oh!, i just thought of the perfect example! for like fading out of the image to black! so that it doesnt do it in a millisecond, and it doesnt keep restarting! right?

Quantum
Quantum

This topic is closed to new replies.

Advertisement