newbie question on singleton class

P

pauldepstein

I liked this article by Danny Kalev on the singleton design class:
http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp

However I was confused by this:

QUOTE BEGINS HERE
The class's implementation looks like this:


Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}

QUOTE ENDS HERE

My confusion stems from the fact that pinstance is a private static
member of Singleton.

So I would have thought that Singleton* Singleton::pinstance = 0;
violates private access.

What am I missing?

Thanks,

Paul Epstein
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I liked this article by Danny Kalev on the singleton design class:
http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp

However I was confused by this:

QUOTE BEGINS HERE
The class's implementation looks like this:


Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}

QUOTE ENDS HERE

My confusion stems from the fact that pinstance is a private static
member of Singleton.

So I would have thought that Singleton* Singleton::pinstance = 0;
violates private access.

What am I missing?

That's the only way to initialize a static member, since they have to be
initialized before the rest of the program is run.
 
K

kingfox

My confusion stems from the fact that pinstance is a private static
member of Singleton.

So I would have thought that Singleton* Singleton::pinstance = 0;
violates private access.

What am I missing?

Thanks,

Paul Epstein

"Singleton* Singleton::pinstance = 0" is only a initialization
statement for the static member Singleton::pinstance. It isn't a
assignment statement. So there is no violates.
 
J

James Kanze

I liked this article by Danny Kalev on the singleton design class:http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp
However I was confused by this:
QUOTE BEGINS HERE
The class's implementation looks like this:
Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}
QUOTE ENDS HERE
My confusion stems from the fact that pinstance is a private static
member of Singleton.
So I would have thought that Singleton* Singleton::pinstance = 0;
violates private access.

Why? It's the definition of pinstance, so it is a member of
Singleton. And it doesn't access anything (but it could---since
pinstance is a member, its initialization has the right to
access private data).
 
G

Gianni Mariani

I liked this article by Danny Kalev on the singleton design class:http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp

However I was confused by this:

QUOTE BEGINS HERE
The class's implementation looks like this:

Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}

A much easier way to write this is:


Singleton* Singleton::Instance ()
{
static Singleton* pinstance = new Singleton;
return pinstance; // address of sole instance
}

This will also generate thread safe code on some compilers.
....
So I would have thought that Singleton* Singleton::pinstance = 0;
violates private access.

The initialization expression is in the scope of the class. i.e.

class foo
{
static const unsigned int x = 1;

static unsigned int y;
};

unsigned int foo::y = x; // note not foo::x - just x
 
J

James Kanze

On May 12, 11:53 pm, (e-mail address removed) wrote:
A much easier way to write this is:
Singleton* Singleton::Instance ()
{
static Singleton* pinstance = new Singleton;
return pinstance; // address of sole instance
}
This will also generate thread safe code on some compilers.
...

So it is claimed. In fact, I only know of one compiler that
even tries, and it fails on my most important target (Solaris on
Sparc).

In practice, what I usually do is to leave the instance variable
outside of the function (more often in an anonymous namespace
than as a static member), and write something like:

Singleton* ourInstance = Singleton::instance() ;

This ensures (in practice, anyway) that the Singleton::instance
function is called before entering main, and thus before I've
created any threads. And once the pointer is non-null, it is
never modified, so (at least according to Posix), there is no
longer any need to protect accesses. (Note that this code is a
little tricky, since it counts on zero initialization.)
The initialization expression is in the scope of the class.

And doesn't access any private variables anyway:).
 
G

Gianni Mariani

So it is claimed. In fact, I only know of one compiler that
even tries, and it fails on my most important target (Solaris on
Sparc).

What is that bug number you filed ?
In practice, what I usually do is to leave the instance variable
outside of the function (more often in an anonymous namespace
than as a static member), and write something like:

Singleton* ourInstance = Singleton::instance() ;

This ensures (in practice, anyway) that the Singleton::instance
function is called before entering main, and thus before I've
created any threads. And once the pointer is non-null, it is
never modified, so (at least according to Posix), there is no
longer any need to protect accesses. (Note that this code is a
little tricky, since it counts on zero initialization.)

Nice if it works but it fails the order of initialization demands if
you have a more than trivial set of initializations to do.
And doesn't access any private variables anyway:).

Are you saying that foo::x in my example is not private ? If you are
you're wrong and if you're not I have no idea what you're trying to
say.
 

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,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top