Thread: How to initialize static members?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    16

    How to initialize static members?

    I have to write a class with a static int member but I do not wish to initialize this member until I need to use it in one of my functions.


    Let say i have for example:
    private:
    static int x;

    I tried to initialize this by typing "int Classname::x=0; "in my function and it didn't work.

    So how do I do this??

    Thank you!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream>
    
    
    class X
    {
      private:
      static int i;
    
      public:
      void set(int j) { i = j;}
      int get() {return i;}
    
    };
    
    int X::i;
    
    int main(void)
    {
      X x1, x2;
      x1.set(10);
      x2.set(20);
      std::cout <<"x1: " <<x1.get() <<std::endl;
      std::cout <<"x2: " <<x2.get() <<std::endl;  
      
      return(0);
    }
    
    /* output:
    
    x1: 20
    x2: 20
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    so that initialization is never done within a function but just independently?

    Thank you so much btw!

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What is the purpose of this line: int X::i;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    that line is used to initialize teh static member.

    But i don't see why it is written there

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The static member must be instantiated somewhere. It is only declared inside the class, but must be instantiated once and only once somewhere outside the class. If your class declaration is in a header file, you usually put the instantiation of static variables in the source file. In Hammer's example the variable is not explicitly intialized, although you could initialize it like this:

    int X::i = 20;

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>In Hammer's example the variable is not explicitly intialized
    To clarify, it is implicitly initialised to zero, just like any other static variable, hence I omitted an explicit initialising statement.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM