Singleton problem

H

Harry

Hi ppl

I have a doubt on singleton class. I am writing a program below

class singleton
{
private:
singleton(){};
public:
//way 1
static singleton instance()
{
static singleton s;
return s;
}
//way 2
static singleton* instance()
{
static singleton s;
return &s;
}
};


int main(int argc, char* argv[])
{
//If way1
singleton st1=singleton::instance();
singleton st2=singleton::instance();
printf("\n%08X",&st1);
printf("\n%08X",&st2);
//if way 2
singleton *st1=singleton::instance();
singleton *st2=singleton::instance();
printf("\n%08X",st1);
printf("\n%08X",st2);
return 0;
}

Now my doubts are as follows:
1. the address of st1 and st2 are different for way1
2. the output is correct in way2. The addresses outputted are equal.
So thats a correct implementation of singleton class.
3. I feel that in way1 the compiler should complain. Because its same
as:

singleton st1; //generates compiler error since call to
private CTOR
singleton st2; //generates compiler error since call to private
CTOR
st1=singleton::instance();
st2=singleton::instance();
printf("\n%08X",&st1);
printf("\n%08X",&st2);
But the compiler does not complain. I am using MS VC++ 6.0 compiler.

4. Even if the compiler understands me correctly for the 1st way, i
feel that the addresses should be printed out equal. This is because
the object made inside the static function is also declared static, so
only the 1st call to it will create an object and the rest of calls
wont.

Can someone comment on the 1st way. I know its wrong but i cannot
figure out a valid reason. The 2nd way is the correct way to create
singleton class in C++.

Thank you for your comments in advance
 
V

Victor Bazarov

Harry said:
Hi ppl

I have a doubt on singleton class. I am writing a program below

class singleton
{
private:
singleton(){};

Drop the semicolon and add

singleton(const singleton&);

which should disable construction of a singleton by anybody else even
from the static instance function.
public:
//way 1
static singleton instance()
{
static singleton s;
return s;
}

In this "way" you allow copying of your static singleton (it does not
matter that this copying is done by returning from a member fuction)
which contradicts the singleton principle. You should do

static singleton & instance()
{
static singleton s;
return s;
}
//way 2
static singleton* instance()
{
static singleton s;
return &s;
}
};


int main(int argc, char* argv[])
{
//If way1
singleton st1=singleton::instance();
singleton st2=singleton::instance();
printf("\n%08X",&st1);
printf("\n%08X",&st2);
//if way 2
singleton *st1=singleton::instance();
singleton *st2=singleton::instance();
printf("\n%08X",st1);
printf("\n%08X",st2);
return 0;
}

Now my doubts are as follows:
1. the address of st1 and st2 are different for way1

Of course. They are not singletons any more. Use the reference as I
showed.
2. the output is correct in way2. The addresses outputted are equal.
So thats a correct implementation of singleton class.

_A_ correct implementation.
3. I feel that in way1 the compiler should complain. Because its same
as:

singleton st1; //generates compiler error since call to
private CTOR
singleton st2; //generates compiler error since call to private
CTOR
st1=singleton::instance();
st2=singleton::instance();
printf("\n%08X",&st1);
printf("\n%08X",&st2);
But the compiler does not complain. I am using MS VC++ 6.0 compiler.

4. Even if the compiler understands me correctly for the 1st way, i
feel that the addresses should be printed out equal. This is because
the object made inside the static function is also declared static, so
only the 1st call to it will create an object and the rest of calls
wont.

Can someone comment on the 1st way. I know its wrong but i cannot
figure out a valid reason. The 2nd way is the correct way to create
singleton class in C++.

HTH

V
 
D

DaKoadMunky

static singleton instance()
{
static singleton s;
return s;
}

You are returning s by value. That means you are returning a copy of s.

You should return by reference...

static singleton& instance()
{
static singleton s;
return s;
}

....the return value is now a reference that is being initialized with s.
singleton st1=singleton::instance();
singleton st2=singleton::instance();

Even with the change from return by value to return by reference you have a
problem here. Variables st1 and st2 are singleton objects, not singleton
references. The syntax you are using is called copy initialization and
requires the existence of a copy constructor (the actual call to the copy
constructor may be optimized out of existence.)
Now my doubts are as follows:
1. the address of st1 and st2 are different for way1

That is because they are two different objects.
3. I feel that in way1 the compiler should complain. Because its same as:

singleton st1; //generates compiler error since call to private CTOR
singleton st2; //generates compiler error since call to private CTOR
st1=singleton::instance();
st2=singleton::instance();

They are not the same. Lines 1 and 2 use the default constructor for singleton
which is private, hence the compiler error. Lines 3 & 4 do not use the default
constructor...they use the compiler generated public copy constructor, hence
the compile without error.

I get the impression you don't grok the difference between an object and a
reference to an object. Are you coming to C++ from Java?
 
H

Harry

You are returning s by value. That means you are returning a copy of s.

You should return by reference...

static singleton& instance()
{
static singleton s;
return s;
}

...the return value is now a reference that is being initialized with s.


Even with the change from return by value to return by reference you have a
problem here. Variables st1 and st2 are singleton objects, not singleton
references. The syntax you are using is called copy initialization and
requires the existence of a copy constructor (the actual call to the copy
constructor may be optimized out of existence.)


That is because they are two different objects.


They are not the same. Lines 1 and 2 use the default constructor for singleton
which is private, hence the compiler error. Lines 3 & 4 do not use the default
constructor...they use the compiler generated public copy constructor, hence
the compile without error.

I get the impression you don't grok the difference between an object and a
reference to an object. Are you coming to C++ from Java?

Well...thank you ppl. Actually I am a C programmer and do not have
much experiance with C++. This damn thing called reference isnt there
in C. But I have understood u perfectly. I had overlooked the copy
constructor issue.

Regards
Harry
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top