473,761 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2653
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_instance s;
// 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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
return new singleton;
}
else
return null;
}

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

"Deming He" <de*******@worl dnet.att.net> wrote in message
news:mo******** ************@bg tnsc04-news.ops.worldn et.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_instance s;


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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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*******@worl dnet.att.net> wrote in message
news:mo******** ************@bg tnsc04-news.ops.worldn et.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_instance s;
// 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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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_instance s;
// 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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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_ins tance()
{
static singleton the_instance;
return &the_instanc e;
}
but you have no control over when the object is destroyed during program
termination.

Michael Mellor
Jul 22 '05 #5
Jumbo
"Deming He" <de*******@worl dnet.att.net> wrote in message
news:mo******** ************@bg tnsc04-news.ops.worldn et.att.net...
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote in message
news:40****** ********@jpl.na sa.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_instance s;
// 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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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_instance s 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******** *************@d reader7.news.xs 4all.nl...
Jumbo
"Deming He" <de*******@worl dnet.att.net> wrote in message
news:mo******** ************@bg tnsc04-news.ops.worldn et.att.net...
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote in message
news:40****** ********@jpl.na sa.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_instance s;
// 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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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_instance s 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*******@worl dnet.att.net> wrote in message
news:moFQb.1065 8$6O4.316338@bg tnsc04-

[SNIP]


singleton* singleton::get_ a_singleton_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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(voi d) {}

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<nos pam> wrote in message
news:10******** *******@news.mi nx.net.uk...

"Deming He" <de*******@worl dnet.att.net> wrote in message
news:mo******** ************@bg tnsc04-news.ops.worldn et.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_instance s;
// 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_ins tance()
{
if(num_of_insta nces<MAX_INSTAN CE){
++num_of_instan ces;
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

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

Similar topics

7
12479
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) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
1
2453
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 template <class T> class Singleton : public T { public: static T * Instance();
3
2498
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 what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
7
3288
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 avoid it? Purify does not show it has mem leak. // test if singleto class has mem leakage #include <iostream>
3
3926
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
5310
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++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
6
2279
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class Singleton { static Singleton s; int i; Singleton(int x) : i(x) { }
3
18252
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
3
1803
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 constraints/environment are as follows: 1) Everything is single-threaded during static initialization (as in prior to the open brace of main) 2) The environment may be multi-threaded during nominal program execution (within {} of main) 3) I...
0
9554
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...
1
9925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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
6640
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
5266
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...
1
3913
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
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.