TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: Neil Heller
from: Darin McBride
date: 2004-11-10 08:05:54
subject: Creating a singleton

Hello Neil!

Replying to a message of Neil Heller to All:

 NH> Has anybody out there created a singleton?  By that I mean, created a 
 NH> program of which there was only one instance, regardless of how many 
 NH> users were logged onto the system.

Most of the time, a singleton really means one object per process - if you
have two copies of a program running on the system at a time, then you have
two singletons.

 NH> If you have made a singleton, did you do it by making the all the 
 NH> constructors in the system private?

Right.  Something like:

class OneOfMe {
private:
  OneOfMe() {}
  static OneOfMe* singleton;
public:
  static OneOfMe* getInstance() {
    if (singleton == NULL) { singleton = new OneOfMe(); }
    return singleton;
  }
};
// in one .C/.cpp file:
OneOfMe* OneOfMe::singleton;

Obligatory Off-Topic Comment: I do this in perl all the time ;-)

 NH> For that matter, has anyone ever made all available constructors 
 NH> private?

All the time :-)  It allows the class to control object creation rather
than the caller.

Darin

---
* Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 633/267 270
@PATH: 250/102 99 10/345 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.