Simple Question: Where to store global object?
-
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
-
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
The "member variable of CWinApp" makes the most sense to me. HTH :)
-
The "member variable of CWinApp" makes the most sense to me. HTH :)
Ok :-) Is it a good way to store the "DatabaseManager" class as a pointer and do a
new CDatabaseManager(filepath)
inside myCMyApp::InitInstance()
(and an appropriate delete in the destructor)? And then everytime I need it, justdynamic_cast<cmyapp*>(AfxGetApp())->GetMyDatabase()
? Thank you, Niki
-
Ok :-) Is it a good way to store the "DatabaseManager" class as a pointer and do a
new CDatabaseManager(filepath)
inside myCMyApp::InitInstance()
(and an appropriate delete in the destructor)? And then everytime I need it, justdynamic_cast<cmyapp*>(AfxGetApp())->GetMyDatabase()
? Thank you, Niki
-
...or is it better to write a
extern CMyApp theApp;
in every source file needed and use
theApp.GetDabataseManager()
?From a strict technical viewpoint, this solution is faster to execute, and cleaner, because there is no cast involved. I would prefer this, personally.
-
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
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