static member of class

Discussion in 'C++' started by Bo Sun, Dec 25, 2003.

  1. Bo Sun

    Bo Sun Guest

    hi:

    please take a look at the following code fragment:

    class Test{

    public:
    static int i;
    Test() {i = 33;}
    };

    int main()
    {

    int Test::i = 40;

    return 0;

    }

    I am wondering, as to static members, why I cannot assign it values inside
    main function? does this mean that I cannot assign static member values
    anywhere inside a function?

    thanks,

    bo
    Bo Sun, Dec 25, 2003
    #1
    1. Advertising

  2. "Bo Sun" <> wrote...
    > please take a look at the following code fragment:
    >
    > class Test{
    >
    > public:
    > static int i;
    > Test() {i = 33;}


    Setting a static data member in a constructor is a BAD IDEA(tm).

    > };
    >
    > int main()
    > {
    >
    > int Test::i = 40;


    The definition of a static member of a class shall appear at the SAME
    namespace level as the class. Pull this definition outside the 'main'
    function.

    >
    > return 0;
    >
    > }
    >
    > I am wondering, as to static members, why I cannot assign it values inside
    > main function? does this mean that I cannot assign static member values
    > anywhere inside a function?


    You're not assigning it. You're _defining_ it. And you're doing
    it in a wrong place. An assignment would be

    Test::i = 40;

    which is fine, as long as you have it defined anywhere, otherwise
    you will have an "undefined symbol" error.

    Victor
    Victor Bazarov, Dec 25, 2003
    #2
    1. Advertising

  3. Bo Sun wrote in news:p:

    > hi:
    >
    > please take a look at the following code fragment:
    >
    > class Test{
    >
    > public:
    > static int i;
    > Test() {i = 33;}
    > };
    >


    This is a defenition of Test::i with an initialization;

    int Test::i = 40;

    > int main()
    > {
    >
    > int Test::i = 40;


    The obove is illegal (if it were legal it would be an defenition and
    initialization not an assigment, but its just plain wrong).


    This is an assignment, and it is legal.

    Test::i = 40;


    >
    > return 0;
    >
    > }
    >
    > I am wondering, as to static members, why I cannot assign it values
    > inside main function? does this mean that I cannot assign static
    > member values anywhere inside a function?
    >


    You need to seperate some concepts,

    declaration :
    telling the compiler that something exists.

    eg: the "static int i;" in your class Test declaration
    above.

    defenition :
    telling the comiler to create an object.

    eg: int Test::i;

    initialization :
    giving an object its initial value (this is *not* assignment).

    eg: the = 40 part of int Test::i = 40;

    assignment :
    giving an object a new value.

    eg: Test::i = 40; in main() above.


    Note that every defenition is also a declaration. Its allowable
    (sometimes) for an object to be declared multiple times, but it must
    have only one defenition.

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Dec 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,743
    Thomas Weidenfeller
    Oct 12, 2005
  2. Sam
    Replies:
    4
    Views:
    471
    The Directive
    Jan 13, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    803
    Micah Cowan
    Jan 12, 2005
  4. .rhavin grobert
    Replies:
    2
    Views:
    305
    Victor Bazarov
    Aug 21, 2007
  5. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page