Re: unable to call public function in SDI MainFrame from View



Joe

GetParentFrame returns a poitner to a CFrameWnd, which is the parent class
of your main frame class. To access the function on your derived class, you
will need to cast the pointer to a pointer to your CMainFrame class.

eg:
CMainFrame* pMainFrame = (CMainFrame*)GetParentFrame();
pMainFrame->MyFunction();

Note this is is generally regarded as 'bad' C++, because you are assuming
that you know the derived type of the class pointed to by the CFrameWnd
pointer returned by GetFrameWnd. You may be confident that this will always
be the case in your MFC app. However, it's generally a bad aproach - what
happens if you reuse your view class as part of an MDI app, where the parent
frame will be a ChildFrame? (I'll tell you - your app will crash). A better
way would be:

CMainFrame* pMainFrame = dynamic_cast<CMainFrame*>(GetParentFrame());
if (pMainFrame != NULL)
pMainFrame->MyFunction();

This way you can be sure that you have a pointer to the correct class.
However, it's still not structurally that sound, and is usually (but not
always) an indication your app should be structured differently.

Hope this helps

David

"Joe" <noaddress@xxxxxxxxx> wrote in message
news:oX%ke.501$f4.390@xxxxxxxxxxxxxxxxxxxxxxx
>I manually added a public function to the MainFrame of my SDI. In my View I
> called GetParentFrame() to get a pointer to the Frame. When I tried to
> call
> the function in the Frame from the View, I got compile error C2039:
> 'MyFunctionInFrameClass' : is not a member of 'CFrameWnd'.
>
> What am I doing wrong?
>


.



Relevant Pages

  • Re: unable to call public function in SDI MainFrame from View
    ... To access the function on your derived class, ... > will need to cast the pointer to a pointer to your CMainFrame class. ... > be the case in your MFC app. ... > frame will be a ChildFrame? ...
    (microsoft.public.vc.mfc)
  • Re: dynamic_cast problem
    ... > The problem seems to be related to the fact that I dynamically load the ... > The library contains a base class and a derived class and also an extern ... > function that returns a pointer ... > Here is my test app. ...
    (comp.lang.cpp)
  • Re: IOCP critical sections and mutexes
    ... mismatch and invalid 'this' pointer). ... instance) and I get pretty much the same crash. ... I mush have done something stupid in my main thread app. ... If I use a mutex instead it works fine. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: 5.1, Data Corruption, Intel, Oh my! [patch] - Fatal trap 12
    ... > instruction at the return address in that frame. ... > with the frame pointer here and didn't get any useful information with ... I also have to assume that the function pointer is ... > stack pointer. ...
    (freebsd-current)
  • Re: Trouble with malloc().
    ... >> I am having trouble with malloc() again for a PC app I am developing. ... This is the only warning or error I get. ... not-necessarily-compatible pointer type. ...
    (comp.lang.c)