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. Includes

Includes

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
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.
  • D Offline
    D Offline
    Dennis Gourjii
    wrote on last edited by
    #1

    Two classes (declared in different headers and defined in different *.cpp files) use each other. How do I #include them in each other's headers correctly? When I use #pragma once it doesn't compile, but without it the headers get infinitely included.:-O Tnx in advance.

    J 1 Reply Last reply
    0
    • D Dennis Gourjii

      Two classes (declared in different headers and defined in different *.cpp files) use each other. How do I #include them in each other's headers correctly? When I use #pragma once it doesn't compile, but without it the headers get infinitely included.:-O Tnx in advance.

      J Offline
      J Offline
      J B 0
      wrote on last edited by
      #2

      If the shared data are simple types such as char, int etc., wouldn't it be better to use a "global" header and place them there, or use extern. If the data are more complicated types (e.g. CEdit), my suggestion would be to just pass the desired data (e.g. CEdit's text - CString) between the two classes.

      D 1 Reply Last reply
      0
      • J J B 0

        If the shared data are simple types such as char, int etc., wouldn't it be better to use a "global" header and place them there, or use extern. If the data are more complicated types (e.g. CEdit), my suggestion would be to just pass the desired data (e.g. CEdit's text - CString) between the two classes.

        D Offline
        D Offline
        Dennis Gourjii
        wrote on last edited by
        #3

        Each of the two classes must contain a pointer to the other one. I know it can be done, but how? The just need to be included.

        R 1 Reply Last reply
        0
        • D Dennis Gourjii

          Each of the two classes must contain a pointer to the other one. I know it can be done, but how? The just need to be included.

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          You can forward declare the pointed class in your .h file. For example, if class A needs to point to class B, do this (in a.h):

          class B;

          class A {
          ...
          public:
          B* m_pointerToB;
          }

          /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

          D 1 Reply Last reply
          0
          • R Ravi Bhavnani

            You can forward declare the pointed class in your .h file. For example, if class A needs to point to class B, do this (in a.h):

            class B;

            class A {
            ...
            public:
            B* m_pointerToB;
            }

            /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

            D Offline
            D Offline
            Dennis Gourjii
            wrote on last edited by
            #5

            Tnx a lot. It does work.

            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