title: where should I declare a database object
body: <p>I am using the Qt libraries in a C++ project but I have a design question: where should a database be declared? I would prefer not to declare global variables.</p>
  
  <p>Currently I am dealing with this problem in this way. I have a mainwindow and I have declared the DB in there so I perform the queries in the main window and pass the results to the dialogs using different signals and slots.</p>
  
  <p>I start the DB when the main window starts and close it when the window has been closed. I don't know if this is ok
  Now I need the DB connection in another class as well so I can pass a reference to the DB or make the DB global</p>
  
  <p>I don't like these solutions.. is there a standard pattern to deal with this situation?</p>
  
  <p>edit:</p>
  
  <p>My class now looks like:</p>
  
  <pre><code>class Database
  {
    public:
      bool open(void);
      bool close(void);
      static Database* getDatabase(void);
      // various methods like loadThisTable(), saveThisTable() etc
  
    private:
      Database();                                // disable constructor
      ~Database();                               // disable destructor
      Database(const Database&amp;);                 // disable copy constructor
      Database&amp; operator=(const Database&amp;);      // disable assignment
  
      static Database* instance_;                // database instance
      QSqlDatabase qtDB;                         // qt db database
  }
  </code></pre>
  
  <p>If I want I can add the add and remove methods but I have a single DB instance.</p>