title: How to solve the problem of global access?
body: <p>I'm building an app, and I need the wisdom of the SO community on a design issue.</p>
  
  <p>In my application, there needs to be EXACTLY one instance of the class <code>UiConnectionList</code>, <code>UiReader</code> and <code>UiNotifier</code>.</p>
  
  <p>Now, I have figured two ways to do this:</p>
  
  <p><strong>Method 1:</strong>
  Each file has a global instance of that class in the header file itself.</p>
  
  <p><strong>Method 2:</strong> there is a separate globals.h file that contains single global instances of each class.
  <br/></p>
  
  <h2>Example code:</h2>
  
  <p><strong>Method 1</strong></p>
  
  <p>file: <code>uiconnectionlist.h</code></p>
  
  <pre><code>#ifndef UICONNECTIONLIST_H
  #define UICONNECTIONLIST_H
  
  #include &lt;QObject&gt;
  #include &lt;QList&gt;
  
  class UiConnection;
  
  class UiConnectionList : public QObject
  {
      Q_OBJECT
  public:
      UiConnectionList();
  
      void addConnection(UiConnection* conn);
      void removeConnection(UiConnection* conn);
  private:
      QList&lt;UiConnection*&gt; connList;
  };
  
  namespace Globals {
      UiConnectionList connectionList;
  }
  
  #endif // UICONNECTIONLIST_H
  </code></pre>
  
  <p>file: <code>uinotifier.h</code></p>
  
  <pre><code>#ifndef UINOTIFIER_H
  #define UINOTIFIER_H
  
  class UiNotifier
  {
  public:
      UiNotifier();
  };
  
  namespace Globals {
      UiNotifier uiNotifier;
  }
  
  #endif // UINOTIFIER_H
  </code></pre>
  
  <p><strong>Method 2:</strong></p>
  
  <p>file: <code>uiconnectionlist.h</code></p>
  
  <pre><code>#ifndef UICONNECTIONLIST_H
  #define UICONNECTIONLIST_H
  
  #include &lt;QObject&gt;
  #include &lt;QList&gt;
  
  class UiConnection;
  
  class UiConnectionList : public QObject
  {
      Q_OBJECT
  public:
      UiConnectionList();
  
      void addConnection(UiConnection* conn);
      void removeConnection(UiConnection* conn);
  private:
      QList&lt;UiConnection*&gt; connList;
  };
  
  #endif // UICONNECTIONLIST_H
  </code></pre>
  
  <p>file: <code>uinotifier.h</code></p>
  
  <pre><code>#ifndef UINOTIFIER_H
  #define UINOTIFIER_H
  
  class UiNotifier
  {
  public:
      UiNotifier();
  };
  
  #endif // UINOTIFIER_H
  </code></pre>
  
  <p>file: <code>globals.h</code></p>
  
  <pre><code>#ifndef GLOBALS_H
  #define GLOBALS_H
  
  #include "uiconnectionlist.h"
  #include "uinotifier.h"
  
  namespace Globals {
      UiConnectionList connectionList;
      UiNotifier uiNotifier;
  }
  
  #endif // GLOBALS_H
  </code></pre>
  
  <h2>My Question</h2>
  
  <p>What is the better/right way to do this?</p>
  
  <p>PS: I don't think that singleton is the right answer here, is it?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>Okay, so two answers have told me to make instances of <code>UiConnectionList</code> and <code>UiNotifier</code>, optionally wrap it in a <code>UiContext</code> and pass it around wherever required.</p>
  
  <p>Could someone enumerate reasons (with examples) why passing around the context is better than having globally accessible variables.</p>
  
  <p>This will help me judge what method is better (or better suited for my app).</p>
  
  <p>Thanks</p>