Thread: static question

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Taipei
    Posts
    70
    Rep Power
    11

    static question


    class A{
    public:
    static int x;
    static void p(); // this static related to lifetime or scope?
    };
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    static meaning there is only one set of code/variables for all instances of the class. Typically you see this in a all static class, it is not usually very smart to mix and match in a class.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    You can call A::p() without instantiating an object of class A.

    p() may not therefore access non static member data (since it is not instantiated).

    Static member data is common to all instances of an object, if one object changes it, it is changed for all instances. (useful for counting instantiations for example).

    Clifford.

IMN logo majestic logo threadwatch logo seochat tools logo