Static attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JoderCoder
    New Member
    • Feb 2008
    • 18

    Static attribute

    Hello all,

    say i have a static attribute -say static int counter- in a class which will be common for all the objects. But how can i give the initial value -say zero- to it?

    thanks much!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    If you always want this to be instantiated at 0 for every occurrence of this object, then you should do it in the class definition.

    Otherwise, have you tried setting it when you create the object?

    Comment

    • JoderCoder
      New Member
      • Feb 2008
      • 18

      #3
      it wouldnt let me do this in the class definition:

      static int counter = 0;

      every new object should be able to manipulate counter, but before manipulating it should know the previous value -i.e. it is common for all objects-

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        For a class defined in a header file

        [code=cpp]
        class MyClass
        {
        private:
        static int counter;

        public:
        MyClass();
        ~MyClass();

        // More methods and data members
        }
        [/code]

        in a ccp file somewhere (may be the class implementation file) have

        [code=cpp]
        int MyClass::counte r = 0;
        [/code]

        Comment

        Working...