Re: Member variables don't hold their value in CChildView. Incorrect pointer? - Programmer

This is a discussion on Re: Member variables don't hold their value in CChildView. Incorrect pointer? - Programmer ; In article , "Basil" wrote: > Good day all. > My problem may be simple, but here goes... > The crux of the probem lies within these statements; > > CChildView *pcv = (CChildView*)AfxGetMainWnd(); //pointer to child view > CMainFrame ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Re: Member variables don't hold their value in CChildView. Incorrect pointer?

  1. Re: Member variables don't hold their value in CChildView. Incorrect pointer?

    In article ,
    "Basil" wrote:

    > Good day all.
    > My problem may be simple, but here goes...
    > The crux of the probem lies within these statements;
    >
    > CChildView *pcv = (CChildView*)AfxGetMainWnd(); //pointer to child view
    > CMainFrame *pmf = (CMainFrame*)AfxGetMainWnd(); //pointer to Main Frame
    >



    Use dynamic_cast:

    CChildView *pcv = dynamic_cast(AfxGetMainWnd());
    CMainFrame *pmf = dynamic_cast(AfxGetMainWnd());

    that way, dynamic_cast will return NULL if the object can't legally
    be cast to the type.

    Without dynamic_cast, you are just confusing yourself.

  2. Re: Member variables don't hold their value in CChildView. Incorrect pointer?


    "David Phillip Oster" wrote in message
    newsster-6960E2.08383430072003@newssvr21-ext.news.prodigy.com...
    > In article ,
    > "Basil" wrote:
    >
    > > Good day all.
    > > My problem may be simple, but here goes...
    > > The crux of the probem lies within these statements;
    > >
    > > CChildView *pcv = (CChildView*)AfxGetMainWnd(); //pointer to child view
    > > CMainFrame *pmf = (CMainFrame*)AfxGetMainWnd(); //pointer to Main Frame
    > >

    >
    >
    > Use dynamic_cast:
    >
    > CChildView *pcv = dynamic_cast(AfxGetMainWnd());
    > CMainFrame *pmf = dynamic_cast(AfxGetMainWnd());
    >
    > that way, dynamic_cast will return NULL if the object can't legally
    > be cast to the type.
    >
    > Without dynamic_cast, you are just confusing yourself.


    Ok great, at least now I know that a cast for CChildView returns NULL
    (0x000000...). Why? I don't quite understand, because CMainFrame appears to
    be a pointer to the entire window, which contains CChildView (CChildView is
    constructed within CMainFrame) as follows within :

    public://protected: // control bar embedded members
    CStatusBar m_wndStatusBar;
    //CToolBar m_wndToolBar; //remove ToolBar from window
    CChildView m_wndView;

    I changed the definition to 'public' so that I could have access to the
    variables within the CChildView class via the CMainFrame class.

    Other than that, I'd want to try the function 'CFrameWnd::GetActiveView '.
    This is what I've done:
    Remember: CMainFrame::CFrameWnd as in 'CMainFrm.h'. Turns out CView is
    never used in my app.


    CWnd* pview;
    pview = pmf->GetActiveView();
    // (CMainFrame *pmf = dynamic_cast(AfxGetMainWnd())

    Returns NULL. So there is no active view??? OK.

    How does one use 'theApp' object? A reference book, "Mickey Williams et al,
    Microsoft Visual C++6 Unleashed, SAMS Publishing July 200", uses it as
    follows:

    Accessing Your Document: To find active document, use m_pMainWnd member of
    CWinApp object like this:
    pDoc = theApp.m_pMainWnd.GetActiveDocument();
    ???????
    One can't use the dot operator after m_pMainWnd... that is a pointer. You
    need to use -> right?
    I get the following errors:

    'theApp' : undeclared identifier
    left of '.m_pMainWnd' must have class/struct/union type
    left of '.GetActiveDocument' must have class/struct/union type

    theApp is global is it not???

    When I use 'CWnd *pcv = theApp.m_pMainWnd->' I don't even get the option to
    select 'GetActiveDocument' !




  3. Re: Member variables don't hold their value in CChildView. Incorrect pointer?

    got the myApp sorted out. Only needed to add 'extern CBenchmarkApp theApp;'
    to the top of the file.
    The book is still wrong though. Hate that book

    "David Phillip Oster" wrote in message
    newsster-6960E2.08383430072003@newssvr21-ext.news.prodigy.com...
    > In article ,
    > "Basil" wrote:
    >
    > > Good day all.
    > > My problem may be simple, but here goes...
    > > The crux of the probem lies within these statements;
    > >
    > > CChildView *pcv = (CChildView*)AfxGetMainWnd(); //pointer to child view
    > > CMainFrame *pmf = (CMainFrame*)AfxGetMainWnd(); //pointer to Main Frame
    > >

    >
    >
    > Use dynamic_cast:
    >
    > CChildView *pcv = dynamic_cast(AfxGetMainWnd());
    > CMainFrame *pmf = dynamic_cast(AfxGetMainWnd());
    >
    > that way, dynamic_cast will return NULL if the object can't legally
    > be cast to the type.
    >
    > Without dynamic_cast, you are just confusing yourself.




+ Reply to Thread