pointer to the app
-
Hi i've got an object that I want to use with a number of dialogues, so I declared it in my App class, and I was then going to use a pointer to the app object to acess it in all my dialogues. How do I get the pointer to the app to access the object? At the moment I have something like
CtheApp* m_pApp;
but how do I initialise the pointer to point at the app? I tried something likem_pApp = &theApp;
but I just get the compiler saying that theApp is an undeclared variable but I thought it was a global. Can someone enlighten me as to the way to proceed? Thanks Andy -
Hi i've got an object that I want to use with a number of dialogues, so I declared it in my App class, and I was then going to use a pointer to the app object to acess it in all my dialogues. How do I get the pointer to the app to access the object? At the moment I have something like
CtheApp* m_pApp;
but how do I initialise the pointer to point at the app? I tried something likem_pApp = &theApp;
but I just get the compiler saying that theApp is an undeclared variable but I thought it was a global. Can someone enlighten me as to the way to proceed? Thanks Andy -
CMyApp* theApp = static_cast<CMyApp*>(AfxGetApp());
"..Even my comments have bugs!"
Inspired by Toni78 -
nice one, thanks, but what is the
static_cast
for can't I just useAfxGetApp
on its own? AndyYes, but it will return you a CWinApp pointer, not a CtheApp pointer. That's why it must be cast to the appropriate type.
-
Yes, but it will return you a CWinApp pointer, not a CtheApp pointer. That's why it must be cast to the appropriate type.
DavidCrow wrote: Yes, but it will return you a CWinApp pointer, not a CtheApp pointer Exactly. AfxGetApp() Returns a CWinApp class pointer. If you want to reference your specific class, you have to cast the returned pointer to your application class. This will allow you to access your member functions. The olden way of casting used to be of the form:
CMyClass* pClass = (CMyClass*)(ParentClass);