Upcoming Events
Engage! Expo NYC
2/16 - 2/17 @ New York, NY

GDC 2010
3/9 - 3/13 @ San Francisco, CA

SXSW Interactive Festival
3/12 - 3/16 @ Austin, TX

IEEE Virtual Reality 2010
3/20 - 3/26 @ Waltham, MA

More events...


Quick Stats
6913 people currently visiting GDNet.
2365 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

Events 4 Gamers

  Intel sponsors gamedev.net search:   
Look Up: (916 Terms)
Browse the Dictionary
Section:
Categories:
Audio (80) Business (59) Community (19)
Design (89) Design Patterns (7) File Formats (32)
Games (64) General (83) Graphics (241)
Hardware (54) Network (41) OS (26)
People (30) Programming (143)
Singleton

A singleton is an object that has and can have no more than one instance.

Often, when a singleton is used in C++, the class consists solely of static members and member functions.

class Singleton
{
private:
    static int PrivateData;
public:
    static void PublicFunction();
};

Another way to ensure that an object is a singleton is to set a static member pointer upon instantiation, and to throw an exception if an instance already exists.

//singleton.h
class Singleton
{
private:
    static Singleton* InstancePointer;
public:
    Singleton();
    inline static Singleton* GetInstancePointer(){return(InstancePointer);}
};
//singleton.cpp
Singleton* Singleton::InstancePointer=0;

Sigleton::Singleton()
{
    if(InstancePointer!=0)
    {
        //error--throw exception
    }
    InstancePointer=this;
}
Categories: Design Patterns
Submitted By: TANSTAAFL


Home
About
Contributors
Add Definition


The Game Dictionary™ is a trademark of GameDev.net LLC. No duplication, reproduction, or transmission of the Game Dictionary or its content is allowed without the consent of GameDev.net LLC.