473,397 Members | 1,985 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,397 software developers and data experts.

singleton

Could somebody please help me with the definition of a singleton?
cat singleton.cc

class {
private:
// representation
int A;
int B;
public:
//functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;

Jul 22 '05 #1
10 2613
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?
> cat singleton.cc

class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;


Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;
Jul 22 '05 #2

"Deming He" <de*******@worldnet.att.net> wrote in message
news:mo********************@bgtnsc04-news.ops.worldnet.att.net...
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?
> cat singleton.cc class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;


Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;


This would probably be better as an unsigned int,
since you don't need the possiblity of a negative
number of instances, do you? :-)
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
Better make that:

return NULL;

or

return 0; }
C++ doesn't define any identifier 'null'.


int singleton::num_of_instances = 0;


-Mike
Jul 22 '05 #3

"Deming He" <de*******@worldnet.att.net> wrote in message
news:mo********************@bgtnsc04-news.ops.worldnet.att.net...
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?
> cat singleton.cc

class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;


Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;


You can still create more than one of them i.e:
int main(){
singleton sg1;
singleton sg2;
return 0;
}

I don't believe this is a singleton.
Jul 22 '05 #4
Deming He wrote:
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?
> cat singleton.cc class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;

Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;

The following should stop users creating extra singletons, etc.
singleton() { }
singleton(const singleton&);
singleton& operator=(const singleton&);
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;

It would be better to return a smart pointer cointing the instance
because the singleton class doesn't clean up after itself.

If you only need one singleton then the following is simpler:
singleton* singleton::get_a_singleton_instance()
{
static singleton the_instance;
return &the_instance;
}
but you have no control over when the object is destroyed during program
termination.

Michael Mellor
Jul 22 '05 #5
Jumbo
"Deming He" <de*******@worldnet.att.net> wrote in message
news:mo********************@bgtnsc04-news.ops.worldnet.att.net...
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?

> cat singleton.cc
class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;


Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;

You can still create more than one of them i.e:
int main(){
singleton sg1;
singleton sg2;
return 0;
}

I don't believe this is a singleton.


I'm pretty new to C++ so I may be totally wrong, but doesn't the fact
that num_of_instances is declared as static make sure that there is only
one actual instance of that variable accross all possible instances of
this singleton class?

If so then the code above should work as advertised. No?

grts,

avi

Jul 22 '05 #6

"Avi Bercovich" <av*@sillypages.org> wrote in message
news:40*********************@dreader7.news.xs4all. nl...
Jumbo
"Deming He" <de*******@worldnet.att.net> wrote in message
news:mo********************@bgtnsc04-news.ops.worldnet.att.net...
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...

Could somebody please help me with the definition of a singleton?

> cat singleton.cc
class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;
Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;

You can still create more than one of them i.e:
int main(){
singleton sg1;
singleton sg2;
return 0;
}

I don't believe this is a singleton.


I'm pretty new to C++ so I may be totally wrong, but doesn't the fact
that num_of_instances is declared as static make sure that there is only
one actual instance of that variable accross all possible instances of
this singleton class?

If so then the code above should work as advertised. No?

Well no , you are right that a static variable is global across all
instances of the class but this does not prevent us from creating loads of
instances of that class.
It only keeps count of the new instances the class creates itself.
Therefore that class can only create ONE instance with is static method but
we can create loads of insatnaces of that class. SO the class isn't a
singleton.

A true singleton works using abractation , a bit like COM. Where you cannot
create an instance directly but you can create a pointer to an interface and
then you can call on the interface to create instances. A class factory it's
sometimes known as.

I might get round to trying to post an example but don't have time right
now.I've done this before and need to refresh my memory but it's quite
advanced OOP IIRC.
Jul 22 '05 #7

"Deming He" <de*******@worldnet.att.net> wrote in message
news:moFQb.10658$6O4.316338@bgtnsc04-

[SNIP]


singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}
The problem is that the use is responsible to destroy the created objects.
However, it's a good principle that whatever creates has to destroy!
Furthermore as long as you don't protect your ctor and copy ctor this is no
singleton 'cause there is easy access to instantiate an object!

int singleton::num_of_instances = 0;


Chris
Jul 22 '05 #8

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?
> cat singleton.cc

class {
private:
// representation
int A;
int B;
public:
//functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;


The easiest principle is the so called "Meyers singleton". This simply
returns a reference to a static instance of an object. The following is a
generic implementation of a singleton wrapper which you can use with
whatever object you want. Of course there are some guidelines you should
stick to. The object that becomes a singleton must be derived from
CNonCopyable to prevent sneaky copy construction and so on. Furthermore the
wrapped class must include a friend declaration for the singleton wrapper to
allow access.

template<class T/*, class Tag = void*/>
class CSingleton
{
private:
CSingleton(void) {}
~CSingleton(void) {}

CSingleton( const CSingleton& rhs );
CSingleton& operator=( const CSingleton& rhs);

public:
static T& Instance() { // Meyers singleton
static T m_Instance;
return m_Instance;
}
};

class CNonCopyable {
protected:
CNonCopyable() {};
virtual ~CNonCopyable() {};
private:
CNonCopyable( const CNonCopyable& rhs );
CNonCopyable& operator=(const CNonCopyable& rhs );
};

The following as an example how to implement a wrapped singleton object

class CObj : public CNonCopyable {
protected:
CObj() {};
public:
~CObj() {};
friend class CSingleton<CObj>; // Attention: this must be
provided to enable the singleton wrapper to construct an object
int m_i;
};

Usage:

CObj& MyObj = CSingleton<CObj>::Instance();
// The following should not work as sneaky copies are prevented by
inheritance of CNonCopyable
CObj Sneaky( CSingleton<CObj>::Instance() );

HTH
Chris
Jul 22 '05 #9
Jumbo @uko2.co.uk> <pcr1000011<nospam> wrote in message
news:10***************@news.minx.net.uk...

"Deming He" <de*******@worldnet.att.net> wrote in message
news:mo********************@bgtnsc04-news.ops.worldnet.att.net...
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Could somebody please help me with the definition of a singleton?

> cat singleton.cc
class {
private:
// representation
int A;
int B;
public:
file://functions
const
int& a(void) const { return A; }
int& a(void) { return A; }
const
int& b(void) const { return B; }
int& b(void) { return B; }
} singleton;


Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;


You can still create more than one of them i.e:
int main(){
singleton sg1;
singleton sg2;
return 0;
}

I don't believe this is a singleton.

Yeah, you're correct. Why not add a "private" default cstor for "singleton"
class?
Jul 22 '05 #10

"Deming He" <de*******@worldnet.att.net> wrote in message
news:gi********************@bgtnsc04-news.ops.worldnet.att.net...
Jumbo @uko2.co.uk> <pcr1000011<nospam> wrote in message
news:10***************@news.minx.net.uk...

"Deming He" <de*******@worldnet.att.net> wrote in message
news:mo********************@bgtnsc04-news.ops.worldnet.att.net...
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
> Could somebody please help me with the definition of a singleton?
>
> > cat singleton.cc
> class {
> private:
> // representation
> int A;
> int B;
> public:
> file://functions
> const
> int& a(void) const { return A; }
> int& a(void) { return A; }
> const
> int& b(void) const { return B; }
> int& b(void) { return B; }
> } singleton;
>

Here you go...

class singleton{
enum {MAX_INSTANCE=1};
static int num_of_instances;
// representation
int A;
int B;
public:
// functions
const int& a(void) const { return A; }
int& a(void) { return A; }
const int& b(void) const { return B; }
int& b(void) { return B; }

static singleton* get_a_singleton_instance();
};
singleton* singleton::get_a_singleton_instance()
{
if(num_of_instances<MAX_INSTANCE){
++num_of_instances;
return new singleton;
}
else
return null;
}

int singleton::num_of_instances = 0;
You can still create more than one of them i.e:
int main(){
singleton sg1;
singleton sg2;
return 0;
}

I don't believe this is a singleton.

Yeah, you're correct. Why not add a "private" default cstor for

"singleton" class?

Yes.
Something like this? :

#include <iostream>

class Singleton{
private:
friend class S;
Singleton(){std::cout<<"Singleton constructor"<<'\n';}
~Singleton(){std::cout<<"Singleton destructor"<<'\n';}
Singleton& operator=(const Singleton& rhs){}
Singleton(const Singleton& rhs){}
};

class S{
private:
static int count;
S(){}
public:
static int getCount(){return count;}
static Singleton* CreateSingleton(){
if(!count){
count++;
return new Singleton;
}else return 0;
}
static ReleaseSingleton(Singleton* p){
delete p;
count--;
}
};
int S::count = 0;

int main(){
Singleton* s1 = S::CreateSingleton();
S::ReleaseSingleton(s1);
s1=0;
std::cout<< "No of Singletons: " << S::getCount() << std::endl;
Singleton* s2 = S::CreateSingleton();
Singleton* s3 = S::CreateSingleton();
/*s3 is null*/
std::cout<< "No of Singletons: " << S::getCount() << std::endl;
S::ReleaseSingleton(s2);

return 0;
}
Jul 22 '05 #11

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

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.