473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Singleton problem

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
3 3903
"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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
7
by: ThunderMusic | last post by:
Hi, I have a problem regarding singletons in C#. What I would like to do is the following ClassA is a singleton ClassB inherits from ClassA and is also a Singleton there cannot and instance of...
3
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: Singleton * instance() {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.