Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pointer to CDocument

pointer to CDocument

Scheduled Pinned Locked Moved C / C++ / MFC
oophelptutorialquestionlearning
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raner
    wrote on last edited by
    #1

    Hi A beginner here.We always use GetDocument() function of CView class to retrieve a pointer to the CDocument class. However this time, i need to get the pointer from a CObject derived class. But i've no idea how to do that at all. Can anyone help? Anyway i always wondered whether i should always place my data in the CDocument class. Because OOD seems to dictate that data should be contained in its own relevant classes for encapsulation. However the Document-View architect seem to suggest placing all data in the CDocument class. Any comments to help me resolve this paradox? thks moo

    C 1 Reply Last reply
    0
    • R raner

      Hi A beginner here.We always use GetDocument() function of CView class to retrieve a pointer to the CDocument class. However this time, i need to get the pointer from a CObject derived class. But i've no idea how to do that at all. Can anyone help? Anyway i always wondered whether i should always place my data in the CDocument class. Because OOD seems to dictate that data should be contained in its own relevant classes for encapsulation. However the Document-View architect seem to suggest placing all data in the CDocument class. Any comments to help me resolve this paradox? thks moo

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      raner wrote: Any comments to help me resolve this paradox? there's no paradox. the "document" in doc-view is the data. the doc itself can contain other classes to actually hold the data (like arrays or whatnot), but the CDocument class is (theoretically) supposed to be the embodiment of your data. the view is just a way to look at the data. raner wrote: i need to get the pointer from a CObject derived class the easiest way is to just carry around a pointer to your CDocument. but, you can also get a pointer to the current doc via CFrameWnd::GetActiveDocument (and you can get the frame wnd from CWinApp). -c


      Zzzzz...

      ThumbNailer

      R 1 Reply Last reply
      0
      • C Chris Losinger

        raner wrote: Any comments to help me resolve this paradox? there's no paradox. the "document" in doc-view is the data. the doc itself can contain other classes to actually hold the data (like arrays or whatnot), but the CDocument class is (theoretically) supposed to be the embodiment of your data. the view is just a way to look at the data. raner wrote: i need to get the pointer from a CObject derived class the easiest way is to just carry around a pointer to your CDocument. but, you can also get a pointer to the current doc via CFrameWnd::GetActiveDocument (and you can get the frame wnd from CWinApp). -c


        Zzzzz...

        ThumbNailer

        R Offline
        R Offline
        raner
        wrote on last edited by
        #3

        but my class is CObject derived, so how do i make use of CFrameWnd::GetActiveDocument to import the pointer? thks thks To carry around the pointer, do you mean something like the below codes? In CView class: CPgmDoc* pDoc= (CPgmDoc*) GetDocument(); CMyObject *pMyObject= new CMyObject; pMyObject->SetDocument(pDoc); In CMyObject: void CMyObject::SetDocument(CRainAttDoc *pDocument) { pDocument=pDoc; } thks for the help.Sorry if this seems painfully elementary;P

        C 1 Reply Last reply
        0
        • R raner

          but my class is CObject derived, so how do i make use of CFrameWnd::GetActiveDocument to import the pointer? thks thks To carry around the pointer, do you mean something like the below codes? In CView class: CPgmDoc* pDoc= (CPgmDoc*) GetDocument(); CMyObject *pMyObject= new CMyObject; pMyObject->SetDocument(pDoc); In CMyObject: void CMyObject::SetDocument(CRainAttDoc *pDocument) { pDocument=pDoc; } thks for the help.Sorry if this seems painfully elementary;P

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          yeah something like that. (carrying it around). raner wrote: so how do i make use of CFrameWnd::GetActiveDocument to import the pointer? something like this:

          // get main frame
          CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

          // Get the active MDI child window.
          CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();

          CDocument *pDoc = pChild->GetActiveDocument();

          this only gets the "active" document, so it might not do exactly what you want.


          ILockBytes

          ThumbNailer

          R 2 Replies Last reply
          0
          • C Chris Losinger

            yeah something like that. (carrying it around). raner wrote: so how do i make use of CFrameWnd::GetActiveDocument to import the pointer? something like this:

            // get main frame
            CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

            // Get the active MDI child window.
            CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();

            CDocument *pDoc = pChild->GetActiveDocument();

            this only gets the "active" document, so it might not do exactly what you want.


            ILockBytes

            ThumbNailer

            R Offline
            R Offline
            raner
            wrote on last edited by
            #5

            Hi I'm working on a SDI project.After looking through my program, i still dont know how to adapt it to SDI project, since there's no m_pMainWnd variable in the CWinApp class. And i'm not sure if GetActiveDocument(); is relevant for a SDI? thks thks

            C 1 Reply Last reply
            0
            • R raner

              Hi I'm working on a SDI project.After looking through my program, i still dont know how to adapt it to SDI project, since there's no m_pMainWnd variable in the CWinApp class. And i'm not sure if GetActiveDocument(); is relevant for a SDI? thks thks

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              raner wrote: since there's no m_pMainWnd variable in the CWinApp class here's it is, more generic :

              CWnd *p = AfxGetMainWnd()->GetActiveWindow();
              if (p->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
              {
              CFrameWnd *pfr = (CFrameWnd *)p;
              CDocument *pdoc = pfr->GetActiveDocument();
              }

              -c


              ILockBytes

              ThumbNailer

              1 Reply Last reply
              0
              • C Chris Losinger

                yeah something like that. (carrying it around). raner wrote: so how do i make use of CFrameWnd::GetActiveDocument to import the pointer? something like this:

                // get main frame
                CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

                // Get the active MDI child window.
                CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();

                CDocument *pDoc = pChild->GetActiveDocument();

                this only gets the "active" document, so it might not do exactly what you want.


                ILockBytes

                ThumbNailer

                R Offline
                R Offline
                raner
                wrote on last edited by
                #7

                Hi i tried the easy method of carrying the pointer around as follows.But it is not working. In CView class: CRainAttDoc* pDoc= (CRainAttDoc*) GetDocument(); CMyObject *pMyObject= new CMyObject; pMyObject->SetDocument(pDoc); pMyObject->ShowGraph() In CMyObject.h private: CRainAttDoc* pDoc; In CMyObject.cpp: void CMyObject::ShowGraph() { CSRawDataArray &dataArray = ((CRainAttDoc*)pDoc)->GetRawDataArray(); } void CMyObject::SetDocument(CRainAttDoc *pDocument) { pDoc=(CRainAttDoc*) pDocument; } where CSRawDataArray is a CArray template and GetRawDataArray() is a function of CRainAttDoc which returns a CSRawDataArray. While debugging, the SetDocument function is found to record the pDoc pointer correctly.However when the pgm runs to the ShowGraph() function, the pDoc became incorrect. know what's wrong? thks alot!

                C 1 Reply Last reply
                0
                • R raner

                  Hi i tried the easy method of carrying the pointer around as follows.But it is not working. In CView class: CRainAttDoc* pDoc= (CRainAttDoc*) GetDocument(); CMyObject *pMyObject= new CMyObject; pMyObject->SetDocument(pDoc); pMyObject->ShowGraph() In CMyObject.h private: CRainAttDoc* pDoc; In CMyObject.cpp: void CMyObject::ShowGraph() { CSRawDataArray &dataArray = ((CRainAttDoc*)pDoc)->GetRawDataArray(); } void CMyObject::SetDocument(CRainAttDoc *pDocument) { pDoc=(CRainAttDoc*) pDocument; } where CSRawDataArray is a CArray template and GetRawDataArray() is a function of CRainAttDoc which returns a CSRawDataArray. While debugging, the SetDocument function is found to record the pDoc pointer correctly.However when the pgm runs to the ShowGraph() function, the pDoc became incorrect. know what's wrong? thks alot!

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

                  i don't see anything wrong with that code. ?? -c


                  A | B - it's not a choice.

                  ThumbNailer

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups