non-const static member variale initialization

This is a discussion on non-const static member variale initialization within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Now compiler prevents (at least in Visual Studio 2008) us from initialize non-const static member variable in this ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    non-const static member variale initialization

    Hello everyone,


    Now compiler prevents (at least in Visual Studio 2008) us from initialize non-const static member variable in this way,

    Code:
    Class Foo {
        statuc int i = 200; // compile error, must use const static int i = 200;
        ...
    };
    Anyone know why compiler prevents initialize non-const variable in this way? Any side effects or just a fixed rule from C++ Spec?


    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    it does but you can get it easily enough.
    just put the assignment in the constructor initializer list
    basicly
    Code:
    class Foo
    {
    public:
       Foo();
    private:
       static int i;
    };
    
    Foo::Foo(/*constructor parameters*/):i(200)
    {
       //
    }

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Raigne,


    I just want to initialize the value of the static member variable before any object instances are created. And the value is decided at runtime, so I can not use const.

    Any ideas why compiler blocks us initializing non-const static variable?

    Quote Originally Posted by Raigne View Post
    it does but you can get it easily enough.
    just put the assignment in the constructor initializer list
    basicly
    Code:
    class Foo
    {
    public:
       Foo();
    private:
       static int i;
    };
    
    Foo::Foo(/*constructor parameters*/):i(200)
    {
       //
    }

    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,919
    Because it isn't constant. The value is static, it's a single instance among all instances of the classes, but it's not const, so its value can change.
    The reason const variables must be initialized like you do is becuse their value cannot change. A static variable's value, however, can.
    If you initialized the constant variables value in the initializer, you'd initialize it every time an instance of the class was constructed! So, a const static variable is an exception, while a mere static isn't.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by George2 View Post
    I just want to initialize the value of the static member variable before any object instances are created.
    If it is private - you cannot access it without creating an instance - so what's the problem?
    If it is public - initialize is as any other global variable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    You did not understand my question correctly. My question is not why const static variable must be declared in the way I mentioned, but why non-const variable can not be initialized in the same way how we initialize const static variable.

    Any ideas? Let me know if I have not made myself understood.

    Quote Originally Posted by Elysia View Post
    Because it isn't constant. The value is static, it's a single instance among all instances of the classes, but it's not const, so its value can change.
    The reason const variables must be initialized like you do is becuse their value cannot change. A static variable's value, however, can.
    If you initialized the constant variables value in the initializer, you'd initialize it every time an instance of the class was constructed! So, a const static variable is an exception, while a mere static isn't.

    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    No vart,


    It is public, but we can not initialize in the way below. Do you know why compiler blocks this?

    Code:
    Class Foo {
    public:
        statuc int i = 200; // compile error, must use const static int i = 200;
        ...
    };
    Quote Originally Posted by vart View Post
    If it is private - you cannot access it without creating an instance - so what's the problem?
    If it is public - initialize is as any other global variable

    regards,
    George

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,919
    Quote Originally Posted by George2 View Post
    Thanks Elysia,

    You did not understand my question correctly. My question is not why const static variable must be declared in the way I mentioned, but why non-const variable can not be initialized in the same way how we initialize const static variable.

    Any ideas? Let me know if I have not made myself understood.

    regards,
    George
    I believe I made myself pretty clear. Const static variables are exceptions and must be initialized in a special way. Mere static variables aren't exceptions and must be initialized the normal way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    I think I'd better remember it as a fixed C++ rule. Previously, I am thinking if we initialize non-const static variable in the same way (at least could be one of the ways to initialize non-const static variable) as const static variable, there may be some side effects which I have not thought before.

    Now I understand it is just a fixed C++ rule and non-const static variable follows the same way as normal variables, except for const static variable.

    Quote Originally Posted by Elysia View Post
    I believe I made myself pretty clear. Const static variables are exceptions and must be initialized in a special way. Mere static variables aren't exceptions and must be initialized the normal way.

    regards,
    George

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,919
    If you think about it logically, there's a reason you can't initialize const static variables in the initializer.
    Because for every instance of that object you create, you will initialize the const static variable again. But since there's only one instance of it and it's const, it must be initialized only once.
    So that's why it's an exception.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by George2 View Post
    No vart,


    It is public,
    Then

    Code:
    class Foo {
    public:
        static int i; 
    };
    
    int Foo::i = 200;
    just stick with it - or switch to C#
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    OK, there's far too much nonsense here.

    1) You can't initialize any static members in the initializer list. Initializing means giving it an initial value. You can't do that in the per-object constructor for a static variable, obviously.

    2) The basic form of initialization for all statics is out-of-line:
    Code:
    class foo
    {
      static int bar;
    };
    int foo::bar; // with an optional initializer here
    3) Only for constant, integral, static members a special rule exists which says that you can initialize them in-line:
    Code:
    class foo
    {
      const static int bar = 200;
    };
    const int foo::bar; // still required, but initializer now forbidden
    4) The reason for #2 is consistency. In short, that's just the way it works in C++: globals (and static members are just globals with limited scope and access rights) are given a value at their definition, not their declaration.

    5) The reason for #3 is again consistency. C++ allows initialization of global integral constants at the declaration, so that the compiler has the actual value at hand for compile-time calculations.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    For code like this,

    Code:
    Class Foo{
    public:
        int i;
        Foo(): i(100)
        {
        }
    };
    Where do you think is declaration and where do you think is definition of variable i?

    Quote Originally Posted by CornedBee View Post
    3) Only for constant, integral, static members a special rule exists which says that you can initialize them in-line:
    Code:
    class foo
    {
      const static int bar = 200;
    };
    const int foo::bar; // still required, but initializer now forbidden
    I do not understand what do you mean "still required"?

    Thanks Elysia,


    Quote Originally Posted by Elysia View Post
    If you think about it logically, there's a reason you can't initialize const static variables in the initializer.
    Because for every instance of that object you create, you will initialize the const static variable again. But since there's only one instance of it and it's const, it must be initialized only once.
    So that's why it's an exception.
    You only explains why we should initialize const static member during declaration, not why we can not initialize non-const statuc member during declaration?

    Thanks vart,

    Quote Originally Posted by vart View Post
    Then

    Code:
    class Foo {
    public:
        static int i; 
    };
    
    int Foo::i = 200;
    just stick with it - or switch to C#
    I know you solution could work. But I am thinking why compiler prevents us from initializing non-const static member variable, whether there are any risk situation which we have never though of -- and compiler will prevent us from such risk situation.


    regards,
    George

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    I know you solution could work. But I am thinking why compiler prevents us from initializing non-const static member variable, whether there are any risk situation which we have never though of -- and compiler will prevent us from such risk situation.


    regards,
    George
    Because the compiler doesn't know where the actual variable will be instantiated at the time of initializer - constants are a different story, as they do not need to change later.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Why Mats?


    If we write this,

    Code:
    Class Foo {
    public:
        static int i = 100;
    };
    compiler should know i should be initialized before creating any instance of variables, it should function good, if not concerning about optimization.

    Why do you say compiler in the case above does not know when to initialize the variable?

    Quote Originally Posted by matsp View Post
    Because the compiler doesn't know where the actual variable will be instantiated at the time of initializer - constants are a different story, as they do not need to change later.

    --
    Mats

    regards,
    George

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 11:17 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 09:46 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21