Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Game Development Dictionary


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;
}



0 Comments


PARTNERS