Sometimes you need to only one instance of a class exist during the program runs.
Here is a Singleton Pattern Implementation in C++.
hedar file:
class Singleton {
private:
static Singleton * single;
Singleton( ) { }
Singleton( const Singleton & instance ) { }
const Singleton & operator = ( const Singleton & instance ) { }
public:
static Singleton * getInstance();
~Singleton( ) { }
};
source file:
Singleton * Singleton::single = NULL;
Singleton * Singleton::getInstance( ) {
if( NULL == single ) {
single = new Singleton( );
}
return single;
}
No comments:
Post a Comment