473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

initialize static members

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

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

Mar 26 '06 #1
4 2533
bo*@coolgroups.com 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
Mar 26 '06 #2
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!!!
Mar 26 '06 #3
anyway, a const static membert can be initialized that way, so
class MySound
{
const static int a = 0;
};

Mar 27 '06 #4
bo****@gmail.com 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
Mar 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual...
3
by: Henryk | last post by:
I'm using the gcc with the -pedantic setting. On my embedded system I need a parameter class that contains all setting for the software. This class needs to be ROM-able to locate it in some kind...
1
by: Alan Johnson | last post by:
Consider the following class template, which has the static member 'seed_'. This template is intended to be inherited by several other classes using the curiously recurring template pattern, so...
9
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
4
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.