oop - C++ Singleton/Active Object Paradigm -


i wondering how make class create several instances i.e

session o1 = new session(); session o2 = new session(); 

you make these sessions active session so.

o1.makeactivesession(); session::setactivesession(o2); 

then @ point in code go:

session::getactivesession(); 

and return active session object or create new 1 if 1 doesn't exist. 1 session can active session @ 1 time, if session told become active session old 1 deactivated.

so question is, how make ?

this lazy-loading based singleton:

class session { public:     static session& getinstance() {         static session s;                   // <-- instantiated upon first call         return s;     }  private:     session() { }                           // <-- private constructor     ~session() { }     session(const session&);                // <-- private copy constructor     session& operator=(const session&);     // <-- private assignment operator }; 

used as:

session& s = session::getinstance(); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -