Static Function & MainFrame 
Author Message
 Static Function & MainFrame

In my MFC application, it was necessary for me to make a static function in
the main window class of the application. From this static function, I need
to call a nonstatic function in the main window object, but I get an error
from the compiler. I tried to get a pointer to the main window using
AfxGetMainWnd(), but it only returns a pointer to a CWnd* .  Since my main
window is derived from CWnd, I was unable to call the nonstatic function.

Is there any way to obtain a pointer to the main window derived from CWnd
without passing a pointer to it to the static function?

--Alex R.



Wed, 10 May 2000 03:00:00 GMT  
 Static Function & MainFrame

Quote:

> In my MFC application, it was necessary for me to make a static function in
> the main window class of the application. From this static function, I need
> to call a nonstatic function in the main window object, but I get an error
> from the compiler. I tried to get a pointer to the main window using
> AfxGetMainWnd(), but it only returns a pointer to a CWnd* .  Since my main
> window is derived from CWnd, I was unable to call the nonstatic function.

> Is there any way to obtain a pointer to the main window derived from CWnd
> without passing a pointer to it to the static function?

> --Alex R.

All you need to do is cast the pointer from AfxGetMainWnd, but this
comes up so often that all my programs have the following global
function:

CMainFrame* AfxGetMyMainWnd()
{  return (CMainFrame*)AfxGetMainWnd();

Quote:
}



Wed, 10 May 2000 03:00:00 GMT  
 Static Function & MainFrame

Quote:


>> In my MFC application, it was necessary for me to make a static function
in
>> the main window class of the application. From this static function, I
need
>> to call a nonstatic function in the main window object, but I get an
error
>> from the compiler. I tried to get a pointer to the main window using
>> AfxGetMainWnd(), but it only returns a pointer to a CWnd* .  Since my
main
>> window is derived from CWnd, I was unable to call the nonstatic function.

>> Is there any way to obtain a pointer to the main window derived from CWnd
>> without passing a pointer to it to the static function?

>> --Alex R.

>All you need to do is cast the pointer from AfxGetMainWnd, but this
>comes up so often that all my programs have the following global
>function:

>CMainFrame* AfxGetMyMainWnd()
>{  return (CMainFrame*)AfxGetMainWnd();
>}

All I'd do to improve this is to use a DYNAMIC_DOWNCAST macro, which check
to see that the pointer is actually pointing to a CMainFrame.

Also, as a mater of style, I'd prefix 'AfxGetMainWnd' with a '::'

Roger



Thu, 11 May 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Static Function & MainFrame

2. One Problem in Calling Static function from Non static function

3. Accessing non-static functions from a static function

4. Virtual &Static function?

5. Pointers to non-static vs static functions

6. Static array containing non static objects and functions

7. static functions and non static variables

8. static functions with non-static variables!

9. C & IBM mainframes (Was: Re: Character Sets)

10. MainFrame& icon

11. Saving/restoring mainframe & view positions/sizes

12. Mainframe Close & WM_QUIT

 

 
Powered by phpBB® Forum Software