pointer to CDocument
-
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
-
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
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...
-
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...
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
-
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
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
-
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
-
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
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
-
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
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.hprivate: 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! -
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.hprivate: 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!