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

Singleton problem

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Harry" <ar******@fht-esslingen.de> wrote...
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
Jul 22 '05 #2

P: n/a
>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?
Jul 22 '05 #3

P: n/a
da*********@aol.com (DaKoadMunky) wrote in message news:<20***************************@mb-m27.aol.com>...
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?


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.