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

I suspect you want to declare a static method in your mainframe class
because you want to pass its address as a callback.  If this is so,
here's a solution:

1  Add the following declarations to your mainfrm.cpp:

     void MyStaticFunction();
     CMainFrame*  pMainFrame = NULL;

     void MyStaticFunction()
     {
       ASSERT (pMainFrame);
       pMainFrame->PublicMethod();
     }

2  In the constructor of CMainFrame, add the lines

      pMainFrame = this;

Now instead of using the static function on your mainframe class,
use the static function MyStaticFunction().  MyStaticFunction() uses
the pMainFrame pointer (initialized when your mainframe window
is constructed) to call your mainframe's non-static methods.

/ravi

"There is always one more bug..."
http://www.tiac.net/users/ravib

--------------

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.



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?

> Yes. Lines marked with >>> differ from AppWizard generated code:

> >>>       static  CMyFrame*       pFrame = NULL;

> >>>       static MyFunc ()
> >>>       {
> >>>               pFrame->MyFrameFunc ();
> >>>       }

>    BOOL CMyApp::InitInstance ()
>    {
>                    :
>                    :
>            m_pMainWnd = new CMyFrame;
> >>>               pFrame = m_pMainWnd;
>    }

This would be OK ... only CMyFrame and CMyApp normally reside in different
source files. Modify the declaration of pFrame to look like this:

(in MYFRAME.CPP:)
        extern  CMyFrame*       pFrame = NULL;

(in MYFRAME.H:)
        extern  CMyFrame*       pFrame;

Please repost or email if it doesn't work.

Juergen

----------------------------------------
****** REMOVE THE .NOSPAM TRAILER ******
----------------------------------------
When a person experiences something that
can't be explained within the scientific
world model, the sensible reaction would
be to re-evaluate the world model rather
than denying the experience.
                         -JMV-
----------------------------------------
****** REMOVE THE .NOSPAM TRAILER ******
----------------------------------------



Sat, 13 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