Thread: static variable

  1. #1

    static 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?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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

  3. #3
    phew.. i was getting worried someone was gonna say no.
    so whats the difference between a static variable and a global variable?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    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

  5. #5
    Registered User
    Join Date
    Jan 2003
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Static variable usage
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 12:33 AM
  3. static variable vs. DLL
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 02-06-2008, 02:15 AM
  4. what is static variable....?
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 04-09-2003, 08:54 AM
  5. Replies: 3
    Last Post: 10-10-2002, 07:34 AM