Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple Question: Where to store global object?

Simple Question: Where to store global object?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasesqlitehelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nobaq
    wrote on last edited by
    #1

    Hi, Where to best store a global object in an SDI app? In my case it's a "DatabaseManager" class for accessing an SQLite database. First it was just a member variable of my CDocument but now I need the object in many places in my app. So where to best store it? * Global variable? * Singleton object? * Member variable of CWinApp? * ... One problem: I want to open the database when constructing the object, so the object may throw an "DatabaseException". For this reason a global variable as well as a singleton seem to be suboptimal to me. Niki

    Richard Andrew x64R D 2 Replies Last reply
    0
    • N nobaq

      Hi, Where to best store a global object in an SDI app? In my case it's a "DatabaseManager" class for accessing an SQLite database. First it was just a member variable of my CDocument but now I need the object in many places in my app. So where to best store it? * Global variable? * Singleton object? * Member variable of CWinApp? * ... One problem: I want to open the database when constructing the object, so the object may throw an "DatabaseException". For this reason a global variable as well as a singleton seem to be suboptimal to me. Niki

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      The "member variable of CWinApp" makes the most sense to me. HTH :)

      N 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        The "member variable of CWinApp" makes the most sense to me. HTH :)

        N Offline
        N Offline
        nobaq
        wrote on last edited by
        #3

        Ok :-) Is it a good way to store the "DatabaseManager" class as a pointer and do a new CDatabaseManager(filepath) inside my CMyApp::InitInstance() (and an appropriate delete in the destructor)? And then everytime I need it, just

        dynamic_cast<cmyapp*>(AfxGetApp())->GetMyDatabase()

        ? Thank you, Niki

        N 1 Reply Last reply
        0
        • N nobaq

          Ok :-) Is it a good way to store the "DatabaseManager" class as a pointer and do a new CDatabaseManager(filepath) inside my CMyApp::InitInstance() (and an appropriate delete in the destructor)? And then everytime I need it, just

          dynamic_cast<cmyapp*>(AfxGetApp())->GetMyDatabase()

          ? Thank you, Niki

          N Offline
          N Offline
          nobaq
          wrote on last edited by
          #4

          ...or is it better to write a

          extern CMyApp theApp;

          in every source file needed and use theApp.GetDabataseManager() ?

          Richard Andrew x64R 1 Reply Last reply
          0
          • N nobaq

            ...or is it better to write a

            extern CMyApp theApp;

            in every source file needed and use theApp.GetDabataseManager() ?

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            From a strict technical viewpoint, this solution is faster to execute, and cleaner, because there is no cast involved. I would prefer this, personally.

            1 Reply Last reply
            0
            • N nobaq

              Hi, Where to best store a global object in an SDI app? In my case it's a "DatabaseManager" class for accessing an SQLite database. First it was just a member variable of my CDocument but now I need the object in many places in my app. So where to best store it? * Global variable? * Singleton object? * Member variable of CWinApp? * ... One problem: I want to open the database when constructing the object, so the object may throw an "DatabaseException". For this reason a global variable as well as a singleton seem to be suboptimal to me. Niki

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              nobaq wrote:

              First it was just a member variable of my CDocument but now I need the object in many places in my app.

              That's what the GetDocument() method is for.

              "Love people and use things, not love things and use people." - Unknown

              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups