473,405 Members | 2,415 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,405 software developers and data experts.

static initializer order

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

class singleton
{
Jan 24 '06 #1
6 4307
de*******@gmail.com wrote:
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.
Jan 24 '06 #2
red floyd wrote:
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!

Jan 24 '06 #3
fe****************@gmail.com wrote:

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.
Jan 24 '06 #4
fe****************@gmail.com wrote:
red floyd wrote:
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!


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;
}
--
Who is General Failure and why is he reading my hard disk?
Jan 24 '06 #5
On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:
fe****************@gmail.com wrote:

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

Jan 24 '06 #6
Jay Nabonne wrote:
On Tue, 24 Jan 2006 09:50:43 +0100, Gabriel wrote:
fe****************@gmail.com wrote:

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


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.

--
Who is General Failure and why is he reading my hard disk?
Jan 25 '06 #7

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
4
by: Gianni Mariani | last post by:
What does the standard say about this code ... #include <iostream> const char * func( const char * v ) { std::cout << "func called with v = " << v << "\n"; return v; }
3
by: boxim | last post by:
got some static props in some classes some of the props need to refer to other static members of the other classes however, cos they're not simple types, cant use const, have to use readonly how...
3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
10
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.