difference static and global variable

hello friends, why we using static variables in a class according to global variable ? are they any special reason ?

 
  
a global can be modified by any piece of code in the program, at any time.
a static exists for the duration (from the time it is created until main exits to end the program) but can be inside a class, or a function, or otherwise scoped such that it is NOT global.

the syntax for static variables in a class requires them to be defined outside the class with a scope. They are not truly global, but they CAN be accessed in a funky way:
classname::variable = value; //no instance of the class required!!!!

they are still scoped to the class. Using them this way as a global is poor coding, same as a global.


you can use a class to wrap up a global, and use static with it as well, to make a safer global variable tool. But its still usually a design flaw. Its all in how you use the code; if you have something that behaves like a global, you probably messed up.
Last edited on
firstly thank you for the answer. What i understand that using global variables are bad most of time. Sorry but i dont understand what you want to say last paragraph("you can use a class...") until "it is all in how you use the code".
Topic archived. No new replies allowed.