static initializer order

D

dewdman42

I have a question for you C++ gurus. Let's say I have a class
singleton class such as:

class singleton
{
.
.
public:
singleton* getInstance();
private:
static singleton* __instance;
.
.
}

singeton.cpp:

singleton* singleton::__instance;

singleton* getInstance()
{
if (__instance==null) {
__instance = new singleton;
}

return __instance;
}

So far so good. Now the question is... some other code in some other
module decides to call singleton::getInstance() during app startup (ie,
before main()). The problem that I am seeing is that if the module
needs to call getInstance() before singleton.cpp has initialized the
static member variable "__instance" then what happens?
 
R

red floyd

I have a question for you C++ gurus. Let's say I have a class
singleton class such as:

class singleton
{
.
.
public:
singleton* getInstance();
private:
static singleton* __instance;
.
.
}

singeton.cpp:

singleton* singleton::__instance;

singleton* getInstance()
{
if (__instance==null) {
__instance = new singleton;
}

return __instance;
}
recommendation:

make instance a static variable inside getInstance(). That will provide
a cleaner way of ensuring initialization.

Also, and this is important, your entire example is undefined behavior.
The standard reserves all identifiers containing a double-underscore
regardless of scope to the implementation (see 17.4.3.1.2).

So try:

class singleton
{
public:
// all other member functions redacted for clarity
static singleton* getInstance();
};

singleton* singleton::getInstance()
{
// of course, you'll need appropriate threading safeguards here
static singleton* instance = 0;
if (!instance)
instance = new singleton;
return instance;
}


In addition, Alexandrescu's "Modern C++ Design" will tell you more than
you ever wanted to know about Singleton implementation.
 
F

ferdinand.stefanus

red said:
recommendation:

make instance a static variable inside getInstance(). That will provide
a cleaner way of ensuring initialization.

Also, and this is important, your entire example is undefined behavior.
The standard reserves all identifiers containing a double-underscore
regardless of scope to the implementation (see 17.4.3.1.2).

So try:

class singleton
{
public:
// all other member functions redacted for clarity
static singleton* getInstance();
};

singleton* singleton::getInstance()
{
// of course, you'll need appropriate threading safeguards here
static singleton* instance = 0;
if (!instance)
instance = new singleton;
return instance;
}


In addition, Alexandrescu's "Modern C++ Design" will tell you more than
you ever wanted to know about Singleton implementation.

Hi

I have one question regarding the singleton implementation: is it
preferable to do like the above implementation or something like:

singleton& singleton::getInstance()
{
static singleton instance;
return instance;
}

Thanks!
 
R

red floyd

I have one question regarding the singleton implementation: is it
preferable to do like the above implementation or something like:

singleton& singleton::getInstance()
{
static singleton instance;
return instance;
}

Given the following disclaimer: I'm just an intermediate hack... I'm not
a guru like Victor, Pete, P.J., or some of the other regulars here; my
recommmendation is:

I'd return the reference. You avoid some issues with initializing
"instance", you don't have to worry about deleteing the pointer, and you
don't have to worry about new throwing std::bad_alloc.

Again, I highly recommend the singleton chapter of MCPPD, even though it
is advanced reading.
 
G

Gabriel

Hi

I have one question regarding the singleton implementation: is it
preferable to do like the above implementation or something like:

singleton& singleton::getInstance()
{
static singleton instance;
return instance;
}

Thanks!

Return the reference (last proposal).

The "pointer version" would make sense if it is a lot of effort/time
needed to construct the instance and the instance isn't needed in every
program run. But even the return a reference:
singleton& singleton::getInstance()
{
// of course, you'll need appropriate threading safeguards here
static singleton* instance = 0;
if (!instance)
instance = new singleton;
return *instance;
}
 
J

Jay Nabonne

Return the reference (last proposal).

The "pointer version" would make sense if it is a lot of effort/time
needed to construct the instance and the instance isn't needed in every
program run. But even the return a reference:
singleton& singleton::getInstance()
{
// of course, you'll need appropriate threading safeguards here
static singleton* instance = 0;
if (!instance)
instance = new singleton;
return *instance;
}

But this version has the same behavior (object only constructed when
getInstance first called), and the object is automatically destroyed as
well.

singleton& singleton::getInstance()
{
static singleton instance;
return instance;
}

Is there any reason to prefer new over a static variable (besides the fact
that the "new"d version will outlive anyone potentially using it; that is,
there are no program termination issues)?

- Jay
 
G

Gabriel

Jay said:
But this version has the same behavior (object only constructed when
getInstance first called), and the object is automatically destroyed as
well.

singleton& singleton::getInstance()
{
static singleton instance;
return instance;
}

Is there any reason to prefer new over a static variable (besides the fact
that the "new"d version will outlive anyone potentially using it; that is,
there are no program termination issues)?

- Jay

Oh yes, you're right. I didn't take enough time to think the problem
through right. So I forgot about destruction and mixed the exact time of
creation up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top