CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Oct 2002
    Posts
    103

    Post 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

  2. #2
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    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.

  3. #3
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275
    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.

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747
    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

  5. #5
    Join Date
    Oct 2002
    Posts
    103
    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.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...simplified
    Code:
    CWinApp *AfxGetApp() { return &theApp; }
    so...no real difference...
    Last edited by Andreas Masur; April 21st, 2003 at 09:35 AM.

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    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
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Ajay Vijay
    I think that code should be:
    Code:
    CWinApp *AfxGetApp() { return &theApp; }
    Thanx
    Yep...thank you for your attention...

  9. #9
    Join Date
    May 2002
    Posts
    1,435
    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;
    }

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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'...

  11. #11
    Join Date
    Apr 2003
    Location
    Oklahoma City, OK
    Posts
    7
    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

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  13. #13
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895
    who cares..
    Just use one of them!!!

  14. #14
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    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.

  15. #15
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    Originally posted by Andreas Masur
    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...
    Function calls does not matter now days? Are you sure? Have you seen the coding, by tracing, behind the AfxGetApp? Well that's a complex one. So it is better to use 'theApp' directly if call is from same executable or use it once store it in a CWinApp pointer, if desired on other executable (DLL for example - on which your app is dependent).
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured