Communication betwwen classes
-
I've got a rookie question. In an SDI or MDI app, in the View Class I can get a pointer to the current instance of the Document class and I don't have to create a new instance of CDocument. So ... If I have two independent classes, what is the procedure, what do I have to do so the classes see eachother without declaring new instances of them ????? Thank you!
-
I've got a rookie question. In an SDI or MDI app, in the View Class I can get a pointer to the current instance of the Document class and I don't have to create a new instance of CDocument. So ... If I have two independent classes, what is the procedure, what do I have to do so the classes see eachother without declaring new instances of them ????? Thank you!
As you said, in your view-class, you have a CDocument-pointer. You can then call functions on the pointer, just as if it were a local variable:
CDocument* pDoc
[needs to hold the pointer to the current Document]pDoc->ADocumentFunction();
But this way you can only access functions of the MFC-CDocument-class, not any extension you made in a derived class CMyDocument. For this, you need to cast. But for that, you need to be sure that you are working with an instance of CMyDocument. But that is easy, unless in very special circumstances where your application can open different type of documents and you have made different classes derived from CDocument. Here is how to cast:CMyDocument* pMyDoc = static_cast(pDoc);
This line tells the compiler to treat your CDocument-pointer as the CMyDocument-pointer that it really is and assign it to the variablepMyDoc
. Subsequently, CMyDoc points to the same object as pDoc, but treats it as a CMyDocument instead as a CDocument (as pDoc does).
Who is 'General Failure'? And why is he reading my harddisk?!?
-
As you said, in your view-class, you have a CDocument-pointer. You can then call functions on the pointer, just as if it were a local variable:
CDocument* pDoc
[needs to hold the pointer to the current Document]pDoc->ADocumentFunction();
But this way you can only access functions of the MFC-CDocument-class, not any extension you made in a derived class CMyDocument. For this, you need to cast. But for that, you need to be sure that you are working with an instance of CMyDocument. But that is easy, unless in very special circumstances where your application can open different type of documents and you have made different classes derived from CDocument. Here is how to cast:CMyDocument* pMyDoc = static_cast(pDoc);
This line tells the compiler to treat your CDocument-pointer as the CMyDocument-pointer that it really is and assign it to the variablepMyDoc
. Subsequently, CMyDoc points to the same object as pDoc, but treats it as a CMyDocument instead as a CDocument (as pDoc does).
Who is 'General Failure'? And why is he reading my harddisk?!?
What I meant is communication between two generic classes, not necessarely the Document and the View class.
-
What I meant is communication between two generic classes, not necessarely the Document and the View class.
Ok, it works just the same. The hardest part is to get a valid pointer of the class you want to access. I typical use an extra constructor parameter, e.g. for access to the Document in dialogs, or special Set-functions (but I then normally make my pointer a const pointer to a const object and carefully test for non-nullness).
Who is 'General Failure'? And why is he reading my harddisk?!?
-
Ok, it works just the same. The hardest part is to get a valid pointer of the class you want to access. I typical use an extra constructor parameter, e.g. for access to the Document in dialogs, or special Set-functions (but I then normally make my pointer a const pointer to a const object and carefully test for non-nullness).
Who is 'General Failure'? And why is he reading my harddisk?!?
Assuming that you know ahead of time which classes need access to each other, you can hardwire it in. Write a function in each class to receive a pointer to the other class (the one it has to communicate with), and in that other class write a function that will send a pointer to itself to the first class. Do this as often as you need to, depending on the number of the classes that need to interact and the required directions of the interaction (i.e, one-way or two-way). Ed