How to pass CMyDoc object or pointer to function of another class called from CMyDoc:
How to pass CMyDoc object or pointer to function of another class called from CMyDoc::Func()
Hello
I want to pass CMyDoc reference or pointer to another class function. How to do this?
Class CMydoc
{
public:
int i;
};
CMyDoc::Func()
{
}
This is class Try and is in different file - Try.cpp:
Class Try
{
FuncTry(CMydoc *pDoc);
};
Try::FuncTry(CMydoc *pDoc)
{
char cc[10];
AfxMessageBox(_itoa(cc,pDoc->i,10));
}
There could be several methods depending of the PLACE WHERE YOU CALL THE Try::FuncTry() method.
1. USING A GLOBAL VARIABLE
- You define a global variable in the MyDoc.cpp file like this:
CMyDoc *pMyDoc;
- In the constructor of the CMyDoc class (file MyDoc.cpp), initialize this global variabile: pMyDoc = this;
- In the file where you call the method Try::FuncTry() , (this could be anyplace in the code, let's assume the file is
called xxx.cpp), declare the pMyDoc variable as external like this: extern CMyDoc *pMyDoc.
- In the file xxx.cpp where u are using the method Try::FuncTry() simply use the global variable pMyDoc:
FuncTry(pMyDoc);
WARNING: U SHOULD BE AWARE HOWEVER THAT IF U DESTROY THE DOCUMENT THE ADRESS POINTED BY THE pMyDoc WON'T BE VALID, SO PAY
ATTENTION !!!
2. I assume that u are using a SDI application. The following code will work just fine for a SDI app. , but if you understand it u will be able to make it work for MDI app too.
I also assumed here that u are calling the TryFunc() from a function called OnTest() in class CMainFrame(). The code will also work without problems even if u are calling the TryFunc() in any other place.
Code:
// get a reference to the CWinApp object from which CMyApp is derived
CWinApp *pMainApp = AfxGetApp();
ASSERT(pMainApp);
// we need this pointer in order to call the GetFirstDocTemplatePosition() function
POSITION pos = pMainApp->GetFirstDocTemplatePosition();
CDocTemplate *pDocTemplate = pMainApp->GetNextDocTemplate(pos);
ASSERT(pDocTemplate);
// at this point we have a reference to the document template, and because we are
// a SDI application we do not continue the iteration
pos = pDocTemplate->GetFirstDocPosition();
CMyDoc *pMyDoc = (CMyDoc *)pDocTemplate->GetNextDoc(pos);
ASSERT(pMyDoc);
// instantiate our Try object and call the FuncTry() method
Try objTry;
objTry.FuncTry(pMyDoc);
That's all. Tell me if it helped, or rate me, or hate me
Note: CMyData is polymorphic base class of three other classes.
So based on pointer available in CMyDoc particular polymorphic subclass's Func function is called.
Thanks for your reply but still my query in second post remains un-answered
i.e. how to pass CMyDoc's pointer to a function of another class in CMyView.cpp
2.0 What should be the function signature or defination?
I tried with following but resulted in error i.e. CMyDoc unresolved identifier even i had included header file of CMyDoc in CMyData
Well....the first question would be what you need the pointer for. And what kind of is the other class. It might not be necessary to pass around the document pointer at all depending on your answers to the former questions. Take a look at the following FAQ.
If you still need to pass the document pointer, you simply use the following signature
Code:
void Func(CMyDocument *pPointer)
{
}
'CMyDocument' needs to be the name of your document class you want to pass (or if you are using RTTI you can use the base class 'CDocument' and cast appropriate).
I have used the base classes 'CView' and 'CMainFrame' in the above sample, they might differ from yours, so you would need to adjust them accordingly. Potentially, you might have to cast the returned document pointer as well...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.