473,837 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3931
"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...t hey 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...t hey 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
2261
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 which makes calls to the service. We are using tcp channel which is using binaryformatter by default. The problem is that after a certain number of remoting calls the calls dont get through to the server. The client application makes the call and...
5
5381
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
1431
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 ClassA and an instance of ClassB, there must be only one of one or the other... Is there a way to do it? or it's simply not possible?
3
435
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() { if(pSingleton==NULL) pSingleton = new Singleton; return pSingleton; }
0
9844
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9683
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9409
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7006
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.