Private Static member object of the same class

V

VSP

Hello,

I am just implementing singleton pattern in various ways. In one
implementation I created a static member and returning that static member in
the getInstance() function. I have made constructor as private and
destructor as public.

class Single
{
private:
Single()
{
}
public:
~Single()
{
}
static Single& getInstance()
{
return s;
}
static Single s;
};
Single Single::s;

This is compiling and linking fine. My question is how the static member
object is getting created and call to the private constructor is getting
succeeded? Who creates the static object? If I make destructor as private
then it gives compilation error, that is obvious. Please explain.

I am using VC++ 6.0

Thanks,
VSP
 
V

Victor Bazarov

VSP said:
I am just implementing singleton pattern in various ways. In one
implementation I created a static member and returning that static
member in the getInstance() function. I have made constructor as
private and destructor as public.

Why do you need a public destructor?
class Single
{
private:
Single()
{
}
public:
~Single()
{
}
static Single& getInstance()
{
return s;
}
static Single s;
};
Single Single::s;

This is compiling and linking fine. My question is how the static
member object is getting created and call to the private constructor
is getting succeeded?

It's created before 'main' is called.
Who creates the static object?

The execution environment.
If I make
destructor as private then it gives compilation error, that is
obvious.

I don't get the error. Why is it obvious?
Please explain.

Hey, if it's obvious to you, why do you ask to explain?
I am using VC++ 6.0

Try getting a better compiler. Download their Express 2005, it's
so much better, you won't believe it.

V
 
S

Sashi

Hello,

I am just implementing singleton pattern in various ways. In one
implementation I created a static member and returning that static member in
the getInstance() function. I have made constructor as private and
destructor as public.

class Single
{
private:
Single()
{
}
public:
~Single()
{
}
static Single& getInstance()
{
return s;
}
static Single s;};

Single Single::s;

This is compiling and linking fine. My question is how the static member
object is getting created and call to the private constructor is getting
succeeded? Who creates the static object? If I make destructor as private
then it gives compilation error, that is obvious. Please explain.

I am using VC++ 6.0

Thanks,
VSP

You need to modify the getInstance() method to first create an object
and then return a pointer/reference to it.
Just google for singleton pattern in C++ and you should see tons of
examples.
Sashi
 
J

James Kanze

Why do you need a public destructor?

Because he's using VC++ 6.0, and that compiler dates to before
the standard. Before the standard, the context in which access
control was applied to the destructor wasn't that clear, and
although it would seem to me that it should be as if in a member
here, most, if not all, compilers back then implemented it as if
the destructor were called at global scope (probably taking the
lead from CFront).

Which "explains" a lot:).
Try getting a better compiler. Download their Express 2005, it's
so much better, you won't believe it.

It's also free:).
 
J

James Kanze

You need to modify the getInstance() method to first create an object
and then return a pointer/reference to it.

Yes and no. If he never accesses the singleton from the
constructor or the destructor of another static object, this
solution works just fine.

Of course, it's just as easy to write a version in which such
accesses also work, and the most of the standard idioms support
them, or at least accesses from constructors of static objects.
 
V

VSP

Hello,

My question is, in the below class, even though the constructor is private,
how can we create a static member object of that class.

Regards,
VSP.
 
M

Mustafa Elsheikh

Hello,

My question is, in the below class, even though the constructor is private,
how can we create a static member object of that class.

Regards,
VSP.

Don't top post! Please reply after the text you are quoting not
before. Top posting messes readability of the post.

The linker-generated runtime startup function _CRTStartup() will call
_static_initialization_and_destruction_0() which in turn will call the
ctor to initialize the static member. I lack the accuracy here, right?
I think the right sequence is _CRTStartup() -> main() -> _GLOBAL_I_() -
_static_initialization_and_destruction_0() -> ctor.
And when you are done with main(), _static_.._destruction_0() is
called again to invoke dtor.
So simply runtime calls main. And before doing anything else it will
call a function to initialize all static members.
 

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,266
Messages
2,571,083
Members
48,773
Latest member
Kaybee

Latest Threads

Top