-
April 20th, 2003, 05:03 PM
#1
theApp vs. AfxGetApp() - does it matter?
Greetings all,
In some MFC samples I see the use of AfxGetApp() to get a pointer to the main app class, in others I see theApp.
Are there any good reasons to use one over the other?
Thanks
Joe
-
April 20th, 2003, 05:16 PM
#2
theApp is the variable created by the VC wizard.
AfxGetApp returns the same instance.
Use either one.
Martin Breton
3D vision software developer and system integrator.
-
April 20th, 2003, 05:18 PM
#3
I think not. Both are the same.
The reason to use AfxGetApp() is, it is global. You can access it from anywhere in your application, while theApp is defined and accessible only inside the application class.
-
April 20th, 2003, 07:52 PM
#4
Originally posted by ergas
The reason to use AfxGetApp() is, it is global. You can access it from anywhere in your application, while theApp is defined and accessible only inside the application class.
theApp is instantiated in the global space as well.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
April 20th, 2003, 09:09 PM
#5
Originally posted by galathaea
theApp is instantiated in the global space as well.
Yeah, I noticed that, but still, you have to add an extern line to YourApp.h because it's declared in YourApp.cpp. Still though, seems like there's not a lot of difference.
-
April 21st, 2003, 09:17 AM
#6
Well...simplified
Code:
CWinApp *AfxGetApp() { return &theApp; }
so...no real difference...
Last edited by Andreas Masur; April 21st, 2003 at 09:35 AM.
-
April 21st, 2003, 09:25 AM
#7
Originally posted by Andreas Masur
Well...simplified
Code:
CWinApp *AfxGetApp() { return theApp; }
so...no real difference...
I think that code should be:
Code:
CWinApp *AfxGetApp() { return &theApp; }
Thanx
-
April 21st, 2003, 09:35 AM
#8
Originally posted by Ajay Vijay
I think that code should be:
Code:
CWinApp *AfxGetApp() { return &theApp; }
Thanx
Yep...thank you for your attention...
-
April 21st, 2003, 10:12 AM
#9
AfxGetApp() is actually defined as below (simplified) which could make a big difference if you are working with DLL's or possibly other unknown 'module state' situations.
CWinApp* AfxGetApp()
{
AfxGetModuleState()->m_pCurrentWinApp;
}
-
April 21st, 2003, 11:24 AM
#10
Originally posted by 0xC0000005
AfxGetApp() is actually defined as below (simplified) which could make a big difference if you are working with DLL's or possibly other unknown 'module state' situations.
CWinApp* AfxGetApp()
{
AfxGetModuleState()->m_pCurrentWinApp;
}
Well...that is the complete implementation of the function...however my intention was to keep things simplified to the OP and to show that there is basically no difference between using the function and/or 'theApp'...
-
April 21st, 2003, 12:58 PM
#11
I think there is a significant difference. Using theApp will be quicker because it doesn't require any function calls, however, it is less portable. So if you are coding something you plan to re-use, you are better off with AfxGetApp().
James
-
April 21st, 2003, 03:21 PM
#12
Originally posted by JamesB43
I think there is a significant difference. Using theApp will be quicker because it doesn't require any function calls, ....
Not necessarely...the functions are implemented as inline functions therefore you will not have any difference in speed...depending on the compiler of course. However, even a function call would not add that much time these days...
-
April 21st, 2003, 03:39 PM
#13
who cares..
Just use one of them!!!
-
April 21st, 2003, 03:43 PM
#14
You can use AfxGetApp in libraries to get hold of the application object where you can't include the .h file for the application.
I know, I've got loads of classes in libraries which do this.
Apparently you can also have COM objects which when AfxGetApp is called return the main application object, not the application object in the COM dll. Don't know how to do it though - but the Visual C++ addin dlls created by ClassWiz seem to be able to do this.
Darwen.
-
April 22nd, 2003, 07:24 AM
#15
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|