if you use a static variable in a class, does that mean that every object instance from that class and derived classes refer to the same variable?
if you use a static variable in a class, does that mean that every object instance from that class and derived classes refer to the same variable?
Yes, that one static variable will hold the same value no matter from which class you access it. You don't even need to instantiate any members of that class to be able to set the value of that variable.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
phew.. i was getting worried someone was gonna say no.
so whats the difference between a static variable and a global variable?
static in a class basically means global (in the scope of that class) just as static in a function means global in the scope of that function.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
Static members have a few advantages over just global members. For example, you can organize your functions and variables better and group them into their related classes. For example, your color class may predefine many colors with static variables. And if you are using templates, you can access that static member with the class type name. Its always best to avoid global members, so place them in a related class if you must use them.
BTW, you can make a brand new namespace for them but if they can be placed in a class, do so.