Thread: question about static members

  1. #1
    Shadow12345
    Guest

    question about static members

    I have a class method that creates a temporary static floating integer, however i'm not sure where it's initialized. It must just be that the static variable is created once the first time the method is called right? Because there doesn't seem to be any special convention for creating it. Thanks.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i take it you are talking about a local static variable in your function such as ...

    void func()
    {
    static float PI=3.14;
    // more stuff
    }

    There the initialisation takes place the first time the function is called and is skipped everytime after. PI holds its value after function exit.

    If on the other hand you mean a static member then it must be initialised outside of the class UNLESS its also const in which case you should be able to do this...

    class A
    {
    static const privmember = 0;
    };

    Your compiler may or may not support that syntax although it is valid c++.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Shadow12345
    Guest
    Ok cool thanks that explains it then. Yes I meant the first thing you were talking about, but the way i worded the title of the thread was ambiguous. So the static member variable isn't initialized upon the constructor, it's initialized 'right there' like in the header file?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no no no
    That is a local static variable not a static member variable. A static member variable must be initialised outside of the class declaration. The only exception to this is if the variable is also const in which case as i said before you can(as long as your compiler supports it) initialise that inside the class declaration.
    Do not confuse member variables with local non-member variables.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  2. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  3. Problems with static const class members
    By sigfriedmcwild in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2004, 07:57 AM
  4. static int class question...
    By ss3x in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2002, 09:00 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM