
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