By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,361 Members | 3,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,361 IT Pros & Developers. It's quick & easy.

Private Static member object of the same class

P: n/a
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
Jun 7 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
VSP wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 7 '07 #2

P: n/a
On Jun 7, 12:03 pm, "VSP" <a...@abc.comwrote:
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

Jun 7 '07 #3

P: n/a
On Jun 7, 6:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
VSP wrote:
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?
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).
I am using VC++ 6.0
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:-).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #4

P: n/a
On Jun 7, 8:04 pm, Sashi <small...@gmail.comwrote:
On Jun 7, 12:03 pm, "VSP" <a...@abc.comwrote:
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;
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #5

P: n/a
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.
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f4**********@news.datemas.de...
VSP wrote:
>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jun 8 '07 #6

P: n/a
On Jun 8, 2:39 pm, "VSP" <a...@abc.comwrote:
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.

"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message

news:f4**********@news.datemas.de...
VSP wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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.

--
Mustafa

Jun 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.