Implementing "Parent" properties in the OO Land
-
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
-
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
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. -
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
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
-
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
Well, a simple answer for you (I guess ) with a big effect for me. Thanks for your help! Matthias