singleton problem

W

wizwx

There are two typical implementations of a singleton. The first one is
to use a static pointer
class Singleton {
static Singleton * pSingleton;
public:
Singleton * instance() {
if(pSingleton==NULL)
pSingleton = new Singleton;
return pSingleton;
}
// ....
};
Singleton * Singleton::pSingleton;

The other is to use a function static
class Singleton {
public:
Singleton& instance() {
static _singleton;
return _singleton;
}
// ....
};

However I just noticed a third one from Bruce Eckel's "Thinking in C+
+" vol. 2, pp 620:
class Singleton {
static Singleton s;
public:
Singleton& instance() {
return s;
}
// ....
};

It is interesting that a static member of itself is declared inside
the Singleton class. I tested the code in linux, and g++ successfully
compiled it.

My question is: How should the compiler know how to allocate memory
for the static member of Singleton? Any features of the keyword
"static" make this feasible?

Thank you for any comments.
 
A

Adrian Hawryluk

wizwx said:
However I just noticed a third one from Bruce Eckel's "Thinking in C+
+" vol. 2, pp 620:
class Singleton {
static Singleton s;
public:
Singleton& instance() {
return s;
}
// ....
};

It is interesting that a static member of itself is declared inside
the Singleton class. I tested the code in linux, and g++ successfully
compiled it.

My question is: How should the compiler know how to allocate memory
for the static member of Singleton? Any features of the keyword
"static" make this feasible?

You have to define the object in your source file like this:

Singleton Singleton::s;

However, it has come to my attention recently about the the static
initialisation order fiasco. See
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12 for more
info. I'm pretty sure that this applies here. I find this fiasco a
real stupid problem, but it is one we have to live with at the moment. :(

Hope this helps.


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
 
G

Greg Herlihy

You have to define the object in your source file like this:

Singleton Singleton::s;

However, it has come to my attention recently about the the static
initialisation order fiasco. See
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12 for more
info. I'm pretty sure that this applies here. I find this fiasco a
real stupid problem, but it is one we have to live with at the moment. :(

No, we don't. Just declare the static instance of the singleton inside the
class static instance() method:

class Singleton
{
public:
static Singleton& instance()
{
static Singleton sInstance;

return sInstance;
}
...
};

This type of singleton (also known as a "Meyers" singleton) has the
advantage of deferring its initialization until the program actually needs
the singleton.

Greg
 
A

Adrian Hawryluk

Greg said:
On 3/21/07 4:28 PM, in article _cjMh.411$6z3.216@edtnps82, "Adrian Hawryluk"


No, we don't. Just declare the static instance of the singleton inside the
class static instance() method:

??? You misunderstand me. I meant that it is because of the singleton
fiasco that we have to live with it by doing the "Meyers" singleton.

I didn't specify it because all the relevant info was in the link
provided. ;)


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
 

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

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top