initialize static members

Discussion in 'C++' started by bob@coolgroups.com, Mar 26, 2006.

  1. Guest

    Why doesn't c++ let me initialize static members like this:

    class MySound
    {
    static CSoundManager* g_pSoundManager = NULL;
    };
     
    , Mar 26, 2006
    #1
    1. Advertising

  2. wrote:
    > Why doesn't c++ let me initialize static members like this:
    >
    > class MySound
    > {
    > static CSoundManager* g_pSoundManager = NULL;
    > };


    Because it's prohibited by the Standard. Did you expect any other
    answer? Do you want to know why it's that way in the Standard?
    You need to ask in comp.std.c++, they discuss _rationales_ behind
    the language features. Here you'll just find complaints about the
    complexity or inconsistencies in the language.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 26, 2006
    #2
    1. Advertising

  3. Phlip Guest

    bob wrote:

    > Why doesn't c++ let me initialize static members like this:
    >
    > class MySound
    > {
    > static CSoundManager* g_pSoundManager = NULL;
    > };


    Because every data item that exists outside of a function must exist in only
    one translation unit. "Exist" means the item's binary image (here containing
    NULL) resides in the translation unit's object file. The linker wants to
    rapidly and efficiently push each item it finds into the output file without
    worrying about what other object files might also declare the same item.

    Without these rules, the linker would see the binary image of
    g_pSoundManager, with its precious NULL, in every object file whose
    translation unit #included any header containing MySound. Then it would need
    to declare one image "the winner", put it into the output binary, and throw
    away all the others. That would waste precious nanoseconds of linker time.
    So we must waste minutes (or more) writing lots of stuff twice, once in a .h
    file and again in a .cpp file.

    If that item were constant, you could initialize it in-situ. That's because
    the linker _does_ see many copies in many object files, then declares one
    the "winner", then optimizes the others away. That optimization allows
    constant abuse, such as via using const_cast<> to change a constant value,
    to cause undefined behavior. The optimizations might not see the new value.

    (Also try sm_ for static member, not g_ for global.)

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
     
    Phlip, Mar 26, 2006
    #3
  4. Guest

    anyway, a const static membert can be initialized that way, so
    class MySound
    {
    const static int a = 0;
    };
     
    , Mar 27, 2006
    #4
  5. wrote:
    > anyway, a const static membert can be initialized that way, so
    > class MySound
    > {
    > const static int a = 0;
    > };


    Yes. And not just 'int', but also a 'short', a 'char', a 'long',
    a 'bool', a 'wchar_t', and unsigned equivalents. But I think you
    need to specify 'static' first. But I can be mistaken.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 28, 2006
    #5
    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. JFCM
    Replies:
    4
    Views:
    5,739
  2. Seb
    Replies:
    2
    Views:
    4,702
  3. r.z.
    Replies:
    6
    Views:
    371
    red floyd
    Mar 22, 2007
  4. Steven Woody
    Replies:
    21
    Views:
    880
    James Kanze
    Jul 20, 2008
  5. Timothy Madden
    Replies:
    1
    Views:
    355
    Timothy Madden
    Nov 21, 2008
Loading...

Share This Page