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. Implementing "Parent" properties in the OO Land

Implementing "Parent" properties in the OO Land

Scheduled Pinned Locked Moved C / C++ / MFC
questiondesignhelptutoriallounge
4 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.
  • M Offline
    M Offline
    Matthias 0
    wrote on last edited by
    #1

    Hello, in the OO-Land (where we all like to live) it's common to implement Object Models, e.g. this one: CCompany >CEmployees >CEmployee For the sake of simplicity we'll say that the company class has a member of type CTypedPtrArray (it's the CEmployees collection). Now I'd like to design my hierarchy so, that each employee has got reference (pointer) to it's parent (company). Now my problem: Let's say I my CCompany looks somewhat like this: #include "employee.h" // my employee class class CCompany{ public: // details ommitted CEmployee& AddEmployee(const CEmployee& newemp, CCompany* parent); private: CTypedPtrArray m_aEmployees; } In this class I need to #include "employee.h". This is fine so far. Now the CEmployee class: #include "company.h" class CEmployee{ // details ommitted private: CCompany* m_ptrParent; } This code doesn't compile. Yes, not even if it is correctly implemented. If you include header A into header B and header B into header A, funny things happen. How do I get this working? What I'm asking for is rather a general answer on how to design such a thing rather then a void*-type-of solution. Many thanx for your time Matthias

    U A M 3 Replies Last reply
    0
    • M Matthias 0

      Hello, in the OO-Land (where we all like to live) it's common to implement Object Models, e.g. this one: CCompany >CEmployees >CEmployee For the sake of simplicity we'll say that the company class has a member of type CTypedPtrArray (it's the CEmployees collection). Now I'd like to design my hierarchy so, that each employee has got reference (pointer) to it's parent (company). Now my problem: Let's say I my CCompany looks somewhat like this: #include "employee.h" // my employee class class CCompany{ public: // details ommitted CEmployee& AddEmployee(const CEmployee& newemp, CCompany* parent); private: CTypedPtrArray m_aEmployees; } In this class I need to #include "employee.h". This is fine so far. Now the CEmployee class: #include "company.h" class CEmployee{ // details ommitted private: CCompany* m_ptrParent; } This code doesn't compile. Yes, not even if it is correctly implemented. If you include header A into header B and header B into header A, funny things happen. How do I get this working? What I'm asking for is rather a general answer on how to design such a thing rather then a void*-type-of solution. Many thanx for your time Matthias

      U Offline
      U Offline
      Uwe Keim
      wrote on last edited by
      #2

      Just declare a forward-reference to the classes. e.g. class CCompany; class CEmployee{ // details ommitted private: CCompany* m_ptrParent; } Then, only in the cpp file, include the "company.h" Since in your classes, you use only references and pointers, the size of the real class is not needed for the compiler.

      1 Reply Last reply
      0
      • M Matthias 0

        Hello, in the OO-Land (where we all like to live) it's common to implement Object Models, e.g. this one: CCompany >CEmployees >CEmployee For the sake of simplicity we'll say that the company class has a member of type CTypedPtrArray (it's the CEmployees collection). Now I'd like to design my hierarchy so, that each employee has got reference (pointer) to it's parent (company). Now my problem: Let's say I my CCompany looks somewhat like this: #include "employee.h" // my employee class class CCompany{ public: // details ommitted CEmployee& AddEmployee(const CEmployee& newemp, CCompany* parent); private: CTypedPtrArray m_aEmployees; } In this class I need to #include "employee.h". This is fine so far. Now the CEmployee class: #include "company.h" class CEmployee{ // details ommitted private: CCompany* m_ptrParent; } This code doesn't compile. Yes, not even if it is correctly implemented. If you include header A into header B and header B into header A, funny things happen. How do I get this working? What I'm asking for is rather a general answer on how to design such a thing rather then a void*-type-of solution. Many thanx for your time Matthias

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Uwe gave you the correct answer. This problem is solved with forward declarations when only a pointer or reference to the object is used. Since pointers and references are always 4 bytes (in Win32) then the compiler can do its thing without caring about what the pointer/reference is for. Regards, Alvaro

        1 Reply Last reply
        0
        • M Matthias 0

          Hello, in the OO-Land (where we all like to live) it's common to implement Object Models, e.g. this one: CCompany >CEmployees >CEmployee For the sake of simplicity we'll say that the company class has a member of type CTypedPtrArray (it's the CEmployees collection). Now I'd like to design my hierarchy so, that each employee has got reference (pointer) to it's parent (company). Now my problem: Let's say I my CCompany looks somewhat like this: #include "employee.h" // my employee class class CCompany{ public: // details ommitted CEmployee& AddEmployee(const CEmployee& newemp, CCompany* parent); private: CTypedPtrArray m_aEmployees; } In this class I need to #include "employee.h". This is fine so far. Now the CEmployee class: #include "company.h" class CEmployee{ // details ommitted private: CCompany* m_ptrParent; } This code doesn't compile. Yes, not even if it is correctly implemented. If you include header A into header B and header B into header A, funny things happen. How do I get this working? What I'm asking for is rather a general answer on how to design such a thing rather then a void*-type-of solution. Many thanx for your time Matthias

          M Offline
          M Offline
          Matthias 0
          wrote on last edited by
          #4

          Well, a simple answer for you (I guess ) with a big effect for me. Thanks for your help! Matthias

          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