473,465 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it simple way to create a singleton or is it wrong?

I've seen codes for singletons in which people write =, copy
constructor, constructor in private section and have some kind of
static member function which returns instance of class. I was able to
understand that.
But today I came across this code which I saw in "Test Your C++ Skills"
by Yashwant Kanetkar. This looks even more simpler but like many other
questions in the book I am sure this must also be having some problems.

I modified the code to add standard headers and changed return type of
main to int.
#include<cstdlib>
#include<iostream>
using namespace std;

class sample{
private:
static int count;
public:
sample(){
if(1==count)
exit(0);
cout<<"Object created\n";
++count;
}
};
int sample::count=0;

int main(){

sample s1;
sample s2;
return 0;
}

Can someone say few points about this?

Nov 1 '06 #1
2 1129
VJ
Kavya wrote:
I've seen codes for singletons in which people write =, copy
constructor, constructor in private section and have some kind of
static member function which returns instance of class. I was able to
understand that.
But today I came across this code which I saw in "Test Your C++ Skills"
by Yashwant Kanetkar. This looks even more simpler but like many other
questions in the book I am sure this must also be having some problems.

I modified the code to add standard headers and changed return type of
main to int.
#include<cstdlib>
#include<iostream>
using namespace std;

class sample{
private:
static int count;
public:
sample(){
if(1==count)
exit(0);
cout<<"Object created\n";
++count;
}
};
int sample::count=0;

int main(){

sample s1;
sample s2;
return 0;
}

Can someone say few points about this?
This is wrong, because:
1) the sample class has no method to get the only instance of the class
2) look at this snip of a code which will break your sample class:

[...]
{
sample a;
// do something if you like
}
[...]

Trying to make another sample object would exit the program
Check http://sourceforge.net/projects/loki-lib/ how it is done
Nov 1 '06 #2

The basic point of a singleton pattern is to limit
the number of instances of a class to 1.
The sample you presented does that but
in a thoroughly useless way.

For an in-depth discussion of a singleton
design I recommend reading
"The modern c++ design"
by Mr. Alexandrescu.

Nov 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
13
by: William Stacey | last post by:
FYI. /// <summary> /// Author: William Stacey /// Fast and simple way to implement a singleton pattern without resorting /// to nested classes or other static vodo. Can also be easily converted...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
15
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static SingletonClass _instance; return &_instance; }
19
by: Brett Romero | last post by:
Here's a table of data I'm putting into a collection: CodeId CodeGroup CodeSubGroup Type 1 K K.1 Shar1 2 K ...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
3
by: gzeng | last post by:
I saw a piece of code from a website. It seems to be a simple example for a singleton class. Basically, the author creates an object in the definition of a class, which has the same name as the...
4
by: DBC User | last post by:
I have a method, in which I populate a local variable from a singleton method value like the following int age = SingletonAge.GetInstance().CurrentAge; then I use age in two places in the...
14
by: Rob Wilkerson | last post by:
Hey all - Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information. What I was surprised to find was that the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.