Connecting Tech Pros Worldwide Forums | Help | Site Map

singleton

E. Robert Tisdale
Guest
 
Posts: n/a
#1: Jul 22 '05
Could somebody please help me with the definition of a singleton?
[color=blue]
> cat singleton.cc[/color]
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;


Deming He
Guest
 
Posts: n/a
#2: Jul 22 '05

re: singleton


E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:40130C40.6050304@jpl.nasa.gov...[color=blue]
> Could somebody please help me with the definition of a singleton?
>[color=green]
> > cat singleton.cc[/color]
> 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;
>[/color]

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;


Mike Wahler
Guest
 
Posts: n/a
#3: Jul 22 '05

re: singleton



"Deming He" <deming.he@worldnet.att.net> wrote in message
news:moFQb.10658$6O4.316338@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
> news:40130C40.6050304@jpl.nasa.gov...[color=green]
> > Could somebody please help me with the definition of a singleton?
> >[color=darkred]
> > > cat singleton.cc[/color]
> > 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;
> >[/color]
>
> Here you go...
>
> class singleton{
> enum {MAX_INSTANCE=1};
> static int num_of_instances;[/color]

This would probably be better as an unsigned int,
since you don't need the possiblity of a negative
number of instances, do you? :-)
[color=blue]
> // 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;[/color]

Better make that:

return NULL;

or

return 0;[color=blue]
> }[/color]

C++ doesn't define any identifier 'null'.

[color=blue]
>
> int singleton::num_of_instances = 0;[/color]

-Mike


Jumbo
Guest
 
Posts: n/a
#4: Jul 22 '05

re: singleton



"Deming He" <deming.he@worldnet.att.net> wrote in message
news:moFQb.10658$6O4.316338@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
> news:40130C40.6050304@jpl.nasa.gov...[color=green]
> > Could somebody please help me with the definition of a singleton?
> >[color=darkred]
> > > cat singleton.cc[/color]
> > 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;
> >[/color]
>
> 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;
>
>[/color]

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.


Michael Mellor
Guest
 
Posts: n/a
#5: Jul 22 '05

re: singleton


Deming He wrote:
[color=blue]
> E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
> news:40130C40.6050304@jpl.nasa.gov...
>[color=green]
>>Could somebody please help me with the definition of a singleton?
>>[color=darkred]
>> > cat singleton.cc[/color]
>> 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;
>>[/color]
>
>
> Here you go...
>
> class singleton{
> enum {MAX_INSTANCE=1};
> static int num_of_instances;
> // representation
> int A;
> int B;[/color]
The following should stop users creating extra singletons, etc.
singleton() { }
singleton(const singleton&);
singleton& operator=(const singleton&);
[color=blue]
> 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;
>[/color]
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
Avi Bercovich
Guest
 
Posts: n/a
#6: Jul 22 '05

re: singleton


Jumbo[color=blue]
> "Deming He" <deming.he@worldnet.att.net> wrote in message
> news:moFQb.10658$6O4.316338@bgtnsc04-news.ops.worldnet.att.net...
>[color=green]
>>E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
>>news:40130C40.6050304@jpl.nasa.gov...
>>[color=darkred]
>>>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;
>>>[/color]
>>
>>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;
>>
>>[/color]
>
>
> 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.[/color]

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

Jumbo
Guest
 
Posts: n/a
#7: Jul 22 '05

re: singleton



"Avi Bercovich" <avi@sillypages.org> wrote in message
news:4013ae13$0$128$e4fe514c@dreader7.news.xs4all. nl...[color=blue]
> Jumbo[color=green]
> > "Deming He" <deming.he@worldnet.att.net> wrote in message
> > news:moFQb.10658$6O4.316338@bgtnsc04-news.ops.worldnet.att.net...
> >[color=darkred]
> >>E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
> >>news:40130C40.6050304@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;
> >>
> >>[/color]
> >
> >
> > 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.[/color]
>
> 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?
>[/color]
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.


Chris Theis
Guest
 
Posts: n/a
#8: Jul 22 '05

re: singleton



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

[SNIP]
[color=blue]
>
>
> singleton* singleton::get_a_singleton_instance()
> {
> if(num_of_instances<MAX_INSTANCE){
> ++num_of_instances;
> return new singleton;
> }
> else
> return null;
> }[/color]

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!
[color=blue]
>
> int singleton::num_of_instances = 0;
>[/color]

Chris


Chris Theis
Guest
 
Posts: n/a
#9: Jul 22 '05

re: singleton



"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:40130C40.6050304@jpl.nasa.gov...[color=blue]
> Could somebody please help me with the definition of a singleton?
>[color=green]
> > cat singleton.cc[/color]
> 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;
>[/color]

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


Deming He
Guest
 
Posts: n/a
#10: Jul 22 '05

re: singleton


Jumbo @uko2.co.uk> <pcr1000011<nospam> wrote in message
news:1075031480.727141@news.minx.net.uk...[color=blue]
>
> "Deming He" <deming.he@worldnet.att.net> wrote in message
> news:moFQb.10658$6O4.316338@bgtnsc04-news.ops.worldnet.att.net...[color=green]
> > E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
> > news:40130C40.6050304@jpl.nasa.gov...[color=darkred]
> > > 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;
> > >[/color]
> >
> > 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;
> >
> >[/color]
>
> 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.
>
>[/color]
Yeah, you're correct. Why not add a "private" default cstor for "singleton"
class?


Jumbo
Guest
 
Posts: n/a
#11: Jul 22 '05

re: singleton



"Deming He" <deming.he@worldnet.att.net> wrote in message
news:giTQb.12805$6O4.399854@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> Jumbo @uko2.co.uk> <pcr1000011<nospam> wrote in message
> news:1075031480.727141@news.minx.net.uk...[color=green]
> >
> > "Deming He" <deming.he@worldnet.att.net> wrote in message
> > news:moFQb.10658$6O4.316338@bgtnsc04-news.ops.worldnet.att.net...[color=darkred]
> > > E. Robert Tisdale <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
> > > news:40130C40.6050304@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;
> > >
> > >[/color]
> >
> > 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.
> >
> >[/color]
> Yeah, you're correct. Why not add a "private" default cstor for[/color]
"singleton"[color=blue]
> class?
>[/color]
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;
}


Closed Thread