Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialize static members

Reply
Thread Tools

initialize static members

 
 
bob@coolgroups.com
Guest
Posts: n/a
 
      03-26-2006
Why doesn't c++ let me initialize static members like this:

class MySound
{
static CSoundManager* g_pSoundManager = NULL;
};

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-26-2006
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!!!


 
Reply With Quote
 
bonczz@gmail.com
Guest
Posts: n/a
 
      03-27-2006
anyway, a const static membert can be initialized that way, so
class MySound
{
const static int a = 0;
};

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-28-2006
(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize static members outside the class Steven Woody C++ 21 07-20-2008 08:40 AM
where/when should I initialize static class data members? r.z. C++ 6 03-22-2007 04:26 AM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
How to Initialize const static object data members in a header file? Seb C++ 2 04-13-2004 02:01 PM



Advertisments