Archived

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

Static Variables

This topic is 5274 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 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 -.-

Share this post


Link to post
Share on other sites
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 here
private:
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."

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
______________________________________________________________

Share this post


Link to post
Share on other sites
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 here

public:
//more stuff
static 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Your definition of a static function is not correct. A static function can access local variables (meaning variables defined inside the function). It also can access static class variables. It cannot, however, access class member variables. Its useful to think of static functions as belonging to the class, not an instance of a class. Thus you can call a static function WITHOUT instantiating the class.

In contrast, a non-static function must work with an instance of a class. However, it can modify both static and nonstatic variables.

To answer a previous posters question (in case you didnt get it)
"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)"

You can''t initialize a static variable inside a constructor because it will not be initialized when used from the class itself. Static variables and functions are usable without instantiating the class, and thus must be defined before the constructor is called.

Share this post


Link to post
Share on other sites