SgDotNet
Singapore Professional .NET User Group -For Cool Developers

help.. what is static variable?

rated by 0 users
This post has 11 Replies | 2 Followers

Top 500 Contributor
Posts 4
slaydragon Posted: 12-12-2006 12:42 AM

hi, i am a noobie in c++.net and i am confused with the use of static variable.

what is static variable and what is it use for ? how is it different from normal variable? wht use static?

would appreciate if given examples.. thanks

Top 10 Contributor
Posts 1,221

A static variable is simply having 1 instance of variable in a program lifetime. 

Difference between a normal variable and static is that a static variable has only 1 instance, a normal variable can have multiple instances.

For example:

static int stat_var;
void foo() {
    int normal_variable;
}
void bar() {
    int normal_variable;
    int stat_var; // error
}

Note that you can use the same name twice in different scopes for normal_variable. For stat_var, there is 1 and only 1 variable name available. If you tried declaring it again, it will give an error.

 When do you want to use a static variable? Obviously when you need only 1 instance of the variable, and share the data across different places. A nicer way of doing global variables. :P

Regards, triplez ------------------------------ http://triplez.mine.nu/blogs
Top 10 Contributor
Posts 2,290

Treat the code you write as a "blueprint" for your class. Whenever you instantiate with new(), a live object of it created. Call new() again and you now have two copies separate of each other - while functionally identical, they don't share the same data.

object1.Name = "Sharon";

object2.Name = "Hyndel";

Now anything that is Static (or "Shared" in Visual Basic) means pertains to the class overall and not to any specific object you may have instantiated during the lifetime of the program. So static members are typically access via class name not the object variable name.

Student.School = "Fog Creek High";  // Not object1 nor object2.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 500 Contributor
Posts 4

rectangle.h

private:
         

Top 500 Contributor
Posts 4

rectangle.h

private:
          static int length = 0;

public:  XXXXXXXXX

^^ //this did not give me an error

 

rectangle.h

private: 
           int length =0;

public:  XXXXXXXXX

^^ //this give me an error

 

why the below on give an error?

 

 

Top 10 Contributor
Posts 2,290

slaydragon:
public:  XXXXXXXXX

I'd like to know what this means. :-) 

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 500 Contributor
Posts 4

erm.. the functions in the public..  use xxxx cos lazy to type out.. the private gives me an error.

Top 10 Contributor
Posts 1,221

Could you paste the exact error generated from the compiler? It's most probably not that problem at all. Or you are just using it wrongly.

Regards, triplez ------------------------------ http://triplez.mine.nu/blogs
Top 10 Contributor
Posts 2,290

slaydragon:
the private gives me an error.

What is this "error" you speak of then? Information and details need to be supplied. As much as we'd like to be omnipresent and omniscient, we're not.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 50 Contributor
Posts 64
slaydragon:

rectangle.h

private:
          static int length = 0;

public:  XXXXXXXXX

^^ //this did not give me an error

 

rectangle.h

private: 
           int length =0;

public:  XXXXXXXXX

^^ //this give me an error

 

why the below on give an error?

 

 

 I believe that the error you are getting for length variable without the static variable is due to accessing without a valid object!. I believe with the actual error it would be easier for US to assist you.

life is like a box of chocolate you never know what you gonna get.
Top 500 Contributor
Posts 5

Your explanation is basically correct, however many important information are missing.

Firstly, to be more exact, we want to differentiate between static variable within a function and within a class.

It would make more sense to say that a static variable within a class is a one-instance member variable. Many objects can be instantiated from the class but only one copy of the variable (static) is available. The value within the static member is accessible and shared by all instances (objects)  of the class. So you ask, in what way is this static member used? Well, it can be used to store data such as rate that is used by all objects of the same class; a common use is to keep a count of how many objects of the same class is instantiated in the system.

A static variable within a function retains value between calls. So when the function exits, the static variable and its value will not be destroyed.

Cheers.

Top 500 Contributor
Posts 5

My friend, don't confuse over programming in C++ and Java.

In Java, you can declare and initialise a member variable, whether static or not, in one statement within the class. But not in C++.

In C++, a static class variable must be declared in a .cpp class implementation file, not in a header file in which you declare your class.

Moreover, static variable can only be modified by static member function.

I enclosed a link for your reading pleasure.

http://www.cprogramming.com/tutorial/statickeyword.html

Page 1 of 1 (12 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems