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. 2 headers files that need to include each other... ?!

2 headers files that need to include each other... ?!

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
7 Posts 4 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.
  • G Offline
    G Offline
    Green Fuze
    wrote on last edited by
    #1

    Hey everybody I have a weird scenario that requires that 2 header file will include each other, and I'm not sure how should I do that. I'll explain... I have 2 Form: mainForm (mainForm.h) and subForm (subForm.h). The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it. Now, the subForm, which is non-modal, can change things on the mainForm (for example, a button that displays text on the mainForm), meaning that subForm must have access to mainForms public functions -> in other words, I must include mainForm.h in subForm. How can I perform something like that in C++ without getting into that problem?! Thanks!

    C K D 3 Replies Last reply
    0
    • G Green Fuze

      Hey everybody I have a weird scenario that requires that 2 header file will include each other, and I'm not sure how should I do that. I'll explain... I have 2 Form: mainForm (mainForm.h) and subForm (subForm.h). The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it. Now, the subForm, which is non-modal, can change things on the mainForm (for example, a button that displays text on the mainForm), meaning that subForm must have access to mainForms public functions -> in other words, I must include mainForm.h in subForm. How can I perform something like that in C++ without getting into that problem?! Thanks!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Green Fuze wrote:

      The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it.

      You can always use a pointer to the dialog class instead of an instance of the dialog class. This way, you only need to put a forward declaration in your header file. See here[^] for instance.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      K G 2 Replies Last reply
      0
      • G Green Fuze

        Hey everybody I have a weird scenario that requires that 2 header file will include each other, and I'm not sure how should I do that. I'll explain... I have 2 Form: mainForm (mainForm.h) and subForm (subForm.h). The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it. Now, the subForm, which is non-modal, can change things on the mainForm (for example, a button that displays text on the mainForm), meaning that subForm must have access to mainForms public functions -> in other words, I must include mainForm.h in subForm. How can I perform something like that in C++ without getting into that problem?! Thanks!

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        Make a common data object which is known to both Forms. If an Update is needed use a user defined message. I think this is also known as MVC (Model-View-Controller) Divide view and data!!! :cool:

        Greetings from Germany

        G 1 Reply Last reply
        0
        • C Cedric Moonen

          Green Fuze wrote:

          The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it.

          You can always use a pointer to the dialog class instead of an instance of the dialog class. This way, you only need to put a forward declaration in your header file. See here[^] for instance.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #4

          pointing from one to the other isnt so clever: if something goes wrong you got an endless loop calling one each other.

          Greetings from Germany

          1 Reply Last reply
          0
          • C Cedric Moonen

            Green Fuze wrote:

            The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it.

            You can always use a pointer to the dialog class instead of an instance of the dialog class. This way, you only need to put a forward declaration in your header file. See here[^] for instance.

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            G Offline
            G Offline
            Green Fuze
            wrote on last edited by
            #5

            Thanks! :-)

            1 Reply Last reply
            0
            • K KarstenK

              Make a common data object which is known to both Forms. If an Update is needed use a user defined message. I think this is also known as MVC (Model-View-Controller) Divide view and data!!! :cool:

              Greetings from Germany

              G Offline
              G Offline
              Green Fuze
              wrote on last edited by
              #6

              Thanks!!! sounds interesting...!

              1 Reply Last reply
              0
              • G Green Fuze

                Hey everybody I have a weird scenario that requires that 2 header file will include each other, and I'm not sure how should I do that. I'll explain... I have 2 Form: mainForm (mainForm.h) and subForm (subForm.h). The mainForm opens a non-modal subForm -> meaning mainForm must include subForm.h in order to show it. Now, the subForm, which is non-modal, can change things on the mainForm (for example, a button that displays text on the mainForm), meaning that subForm must have access to mainForms public functions -> in other words, I must include mainForm.h in subForm. How can I perform something like that in C++ without getting into that problem?! Thanks!

                D Offline
                D Offline
                DevMentor org
                wrote on last edited by
                #7

                what you want to do is include the header files from the source file that will be making use of these functions or classes. having header files include other header files can introduce circular dependencies and this will result in increasing the compile time. this is not a hard and fast rule, but one i follow most of the time. this way you include the source file once, where it is being used!

                --- Yours Truly, The One and Only! web: devmentor.org Design, Code, Test, Deploy

                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