Thread: non-const static member variale initialization

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by George2 View Post
    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?
    The declaration is where it says "int i;"
    The definition as such doesn't exist. The member is defined as part of the larger object whenever a variable of type Foo is defined.

    I do not understand what do you mean "still required"?
    As in: just because the initializer is in the class body, doesn't give you the right to omit the definition.
    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

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but what is the location if Foo::i?

    I don't actually KNOW why it is, I'm only guessing to the reason - if you really want to know, you are probably best asking Bjarne or someone on the C++ standards committee.

    --
    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.

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


    Quote Originally Posted by matsp View Post
    Yes, but what is the location if Foo::i?

    Mats
    I think all static variable should be stored to some global storage, maybe the same as where global variables are stored?

    Thanks CornedBee,


    Could you show us some code about what do you mean definition of a member variable (not declaration), I am confused.

    Quote Originally Posted by CornedBee View Post
    The declaration is where it says "int i;"
    The definition as such doesn't exist. The member is defined as part of the larger object whenever a variable of type Foo is defined.


    As in: just because the initializer is in the class body, doesn't give you the right to omit the definition.

    regards,
    George

  4. #19
    coder
    Join Date
    Feb 2008
    Posts
    127
    A class declaration is the abstraction of an object, it's not the object itself.
    I'm pretty sure we all agree about that.
    Every public or private variable IMO should be initialized when the istance is created: the way I prefer is using the class constructor method.
    c++ = logic, and so it behaves. I believe that c++ developers try to avoid any logical exception, when possible.

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks carlorfeo,


    I agree with your below comments. We are clear about what is member variable declaration and member variable initialization. But unclear what is about member variable definition.

    Could you show us some code about which line of code do you think is member variable definition please? :-)

    Quote Originally Posted by carlorfeo View Post
    A class declaration is the abstraction of an object, it's not the object itself.
    I'm pretty sure we all agree about that.
    Every public or private variable IMO should be initialized when the istance is created: the way I prefer is using the class constructor method.
    c++ = logic, and so it behaves. I believe that c++ developers try to avoid any logical exception, when possible.

    regards,
    George

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    Foo fooInstance;
    This one.

    I think all static variable should be stored to some global storage, maybe the same as where global variables are stored?
    Yes, but the specific compilation unit matters, too.

    Though I don't think that's the actual issue. At least not for static initialization. I think the issue here is simply that by disallowing initialization at the point of declaration, you effectively make it impossible to accidentally give different declarations different values (something that would be possible and nearly undetectable otherwise).
    For dynamic initialization, there's also the order of initialization to worry about.
    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

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


    1.

    Quote Originally Posted by CornedBee View Post
    Code:
    Foo fooInstance;
    This one.
    I have read again the whole post, but I can not find this line of code. I think you mean where is the definition of class instance, right?

    But where is the definition of a specific data member variable of a class, like where is the definition of member variable i (not declaration)?

    Code:
    Class Foo {
    public:
        int i;
        ...
    };
    Quote Originally Posted by CornedBee View Post
    Though I don't think that's the actual issue. At least not for static initialization. I think the issue here is simply that by disallowing initialization at the point of declaration, you effectively make it impossible to accidentally give different declarations different values (something that would be possible and nearly undetectable otherwise).
    For dynamic initialization, there's also the order of initialization to worry about.
    I do not understand what is "different declarations different values" and "the order of initialization to worry about"? Could you show more description of some pseudo code please?


    regards,
    George

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by George2 View Post
    I have read again the whole post, but I can not find this line of code.
    Correct.

    I think you mean where is the definition of class instance, right?

    But where is the definition of a specific data member variable of a class, like where is the definition of member variable i (not declaration)?
    The member is part of the object, so the definition of the object is the definition of the member. That's what I said here:
    The member is defined as part of the larger object whenever a variable of type Foo is defined.
    But apparently you really need to be told everything twice.

    I do not understand what is "different declarations different values" and "the order of initialization to worry about"? Could you show more description of some pseudo code please?
    The order of initialization issue is too complicated for me to come up with a quick example.

    The multiple declarations issue occurs when a stupid programmer doesn't refer to the canonical header file for getting a declaration. I'll use normal globals because it's shorter than whole classes, but the issue is the same:
    Code:
    // foo.hpp
    extern int GLOBAL = 100;
    
    --------------------------
    // foo.cpp
    #include "foo.hpp"
    int GLOBAL;
    
    --------------------------
    // idiot.cpp
    
    // I'm cool, yeah.
    extern int GLOBAL = 200;
    Because the compiler never sees the two declarations at the same time, it can't detect the mismatch. Thus, it's easier to simply forbid initialization at the declaration, of which there can be multiple, and limit it to the definition, of which there can be only one.
    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

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


    Quote Originally Posted by CornedBee View Post
    Code:
    // foo.hpp
    extern int GLOBAL = 100;
    
    --------------------------
    // foo.cpp
    #include "foo.hpp"
    int GLOBAL;
    
    --------------------------
    // idiot.cpp
    
    // I'm cool, yeah.
    extern int GLOBAL = 200;
    Because the compiler never sees the two declarations at the same time, it can't detect the mismatch. Thus, it's easier to simply forbid initialization at the declaration, of which there can be multiple, and limit it to the definition, of which there can be only one.
    I am confused what your samples are about,

    1. In foo.hpp, you define variable GLOBAL? Not declaration since you initialize there;
    2. In foo.cpp, you include foo.hpp, you define GLOBAL and then define another GLOBAL variable, there will be duplicated compile error?
    3. In idiot.cpp, you define another GLOBAL variable. The compile can pass.

    What is your sample about to say?


    regards,
    George

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The lines that have the extern keyword are declarations. In my example, they contain forbidden initializers. This shows that, if declarations could have initializers, these might differ, without the compiler noticing.
    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

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


    1.

    Quote Originally Posted by CornedBee View Post
    The lines that have the extern keyword are declarations. In my example, they contain forbidden initializers.
    I understand this.

    2.

    Quote Originally Posted by CornedBee View Post
    This shows that, if declarations could have initializers, these might differ, without the compiler noticing.
    What do you mean differ?


    regards,
    George

  12. #27
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by George2 View Post
    What do you mean differ?
    In one place is

    static int = 200;

    in the other

    static int = 300;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #28
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi vart,


    We are talking about extern, not static right?

    Quote Originally Posted by vart View Post
    In one place is

    static int = 200;

    in the other

    static int = 300;

    regards,
    George

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by George2 View Post
    Hi vart,


    We are talking about extern, not static right?




    regards,
    George
    I'm talking about static member of the class.
    You can have multiple declarations of the same class in different parts of the project.
    If you put the initialization of static members inside declaration - you got the problem CornedBee has mentioned
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart,


    My question is answered.

    Quote Originally Posted by vart View Post
    I'm talking about static member of the class.
    You can have multiple declarations of the same class in different parts of the project.
    If you put the initialization of static members inside declaration - you got the problem CornedBee has mentioned

    regards,
    George

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, 10:17 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 08: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