VEngineer
asked on
global dialog object
Is it illegal to define a program-global dialog box object? It compiles perfectly, but when I run it, it gives me a runtime debug assertion error.
This is a dialog based app so I have a main dialog class called CTheDlg which is run in a modal loop in CTheApp.
What I need to do is have access (inspect and change) to a data member in CTheDlg throughout the whole program. Is there a better method?
This is a dialog based app so I have a main dialog class called CTheDlg which is run in a modal loop in CTheApp.
What I need to do is have access (inspect and change) to a data member in CTheDlg throughout the whole program. Is there a better method?
Why not try this:
CTheDlg * get_the_only_CTheDlg() {
static CTheDlg dlg(...);
return &dlg;
}
This will delay the initialization until the object is ready to be used and you will not have to explicitly call constructors and destructors. I use this kind of thing a lot and have boilde it down to a template.
template<class T>T& get_the_only() {
static T only_T;
return only_T;
}
and then call it:
class CWhatever {
public:
CWhatever();
public:
func();
};
get_the_only<CWhatever>(). func();
of course, this will only work if you have a constructor that takes no parameters. This obviously will work without MFC.
CTheDlg * get_the_only_CTheDlg() {
static CTheDlg dlg(...);
return &dlg;
}
This will delay the initialization until the object is ready to be used and you will not have to explicitly call constructors and destructors. I use this kind of thing a lot and have boilde it down to a template.
template<class T>T& get_the_only() {
static T only_T;
return only_T;
}
and then call it:
class CWhatever {
public:
CWhatever();
public:
func();
};
get_the_only<CWhatever>().
of course, this will only work if you have a constructor that takes no parameters. This obviously will work without MFC.
oops, sorry. I meant to make that a comment.
ASKER
Ok.. it's a comment again.
ASKER
Let me try some of these options and see which one works out.
My comment is complementary to nietod's. I am agreeing with what he says, just giving an easier way to manage the memory.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the only problem with a global dialog box (or any global object) is thread/concurrency. If you have but one thread (most common case), this is not an issue.
ASKER
Thanks for the help. I found a hybrid solution with everyone's comments.
use AfxGetMainWnd() instead of AfxGetApp()->m_pMainWnd
The solution would be to use a global pointer to a dynamically allocated object. Allocate the object at startup, after MFC is done initializing.