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. Communication betwwen classes

Communication betwwen classes

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 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.
  • B Offline
    B Offline
    BlackRider
    wrote on last edited by
    #1

    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!

    J 1 Reply Last reply
    0
    • B BlackRider

      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!

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      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 variable pMyDoc. 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?!?

      B 1 Reply Last reply
      0
      • J jhwurmbach

        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 variable pMyDoc. 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?!?

        B Offline
        B Offline
        BlackRider
        wrote on last edited by
        #3

        What I meant is communication between two generic classes, not necessarely the Document and the View class.

        J 1 Reply Last reply
        0
        • B BlackRider

          What I meant is communication between two generic classes, not necessarely the Document and the View class.

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          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?!?

          S 1 Reply Last reply
          0
          • J jhwurmbach

            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?!?

            S Offline
            S Offline
            spiritualfields
            wrote on last edited by
            #5

            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

            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