Singleton Class
To create singleton class we need to block normal way of creating class by blocking access to the constructor and provide a new function which will return only one instance of the object.
This function will also perform checking to see if there is only one instance of that object in existence.
The code flow below describe a way to create a Singleton class
Class Singleton {
protected:
// Stops creating any object via normal route
Singleton( );
Singleton* instance( );
Singleton(const Singleton &); // Copy constructor
Singleton & operator= (const Singleton &); // Assignment operator
private:
static Singleton* instance_ptr;
};
// Implementation
Singleton* Singleton::instance_ptr = 0;
Singleton* Singleton::instance ( )
{
if ( *instance_ptr = = 0)
{
Instance_ptr = new Singleton;
return instance_ptr;
}
else
return 0;
}
Similar Posts
No similar recipes.

No comments yet