0

I was wondering how you would make a class where you create several instances i.e

Session o1 = new Session();
Session o2 = new Session();

You could then make these sessions the active session like so.

o1.makeActiveSession();
Session::setActiveSession(o2);

Then at any point in my code I could go:

Session::getActiveSession();

and it would return the active session object or create new one if one doesn't exist. Only one session can be the active session at any one time, so if a session is told to become the active session then the old one is deactivated.

So my question is, how would I make something like this ?

6
  • What's your question? Commented Oct 6, 2013 at 21:12
  • The question is how would I make something that does this behaviour ? or what would you recommend. Commented Oct 6, 2013 at 21:14
  • Just have a global variable and assign to it. More important is why do you want this behavior? There may be a better way. Commented Oct 6, 2013 at 21:17
  • I am wrapping a Facebook Objective-C Library. It requires you to construct session objects and allows you to set them to be the global session in the fashion mentioned above. Commented Oct 6, 2013 at 21:22
  • 1
    Using static members is a clever solution, but if you need this feature, creating a separate session manager class may results more clear structure. Commented Oct 6, 2013 at 21:23

2 Answers 2

3

This is 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();
Sign up to request clarification or add additional context in comments.

1 Comment

This is a classic singleton, but I think the op wants to only allow 1 to be active at a time, but still create multiple instances in a single run.
1

You should be able to take most any of the common Singleton implementations, and modify it to be a manager with CreateNew() and SetActive() functions available.

// Session.h
class ActiveSessionManager;
class Session
{
public:

protected:
  Session(){};

  void MakeActiveSession();

  friend class ActiveSessionManager;
};

// ActiveSessionManager.h
class ActiveSessionManager
{
public:
  static Session *GetActiveSession()
  {
    if ( s_active == nullptr )
    {
      s_active = new Session();
    }

    return s_active;
  }

  static void SetActiveSession( Session *session )
  {
    s_active = session; 
  }

  static Session *CreateNewSession()
  {
    return new Session();
  }

  static Session *CreateNewActiveSession()
  {
    s_active = CreateNewSession();
    return s_active;
  }

private:
  ActiveSessionManager(){};

  static Session *s_active;
};

// I would put these in cpps.
Session *ActiveSessionManager::s_active = nullptr;

void Session::MakeActiveSession()
{
  ActiveSessionManager::SetActiveSession( this );
}

In my implementation I only allow ActiveSessionManager to instance Sessions, as it would then be able to keep track of all sessions generated ( the tracking is left as an exercise for the reader ).

You could combine the manager and session into a single class as well, but I find the separation easier to follow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.