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
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