if you want a class that has only one representation in the proccess
declaration
class Singleton
{
public: static Singleton* Instance();
protected: Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};
implementation
Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{ //... perform necessary instance initializations
}
usage
Singleton *p1 = Singleton::Instance();
Singleton *p2 = p1->Instance();
Singleton & ref = * Singleton::Instance();
Saturday, May 9, 2009
Singleton Design Pattern in c++
תוויות:
c++,
Design Pattern,
Singleton
Subscribe to:
Post Comments (Atom)
2 תגובות:
http://www.naughter.com/
thank you!
this is just what i need for a university project.
bimba.
Post a Comment