Mutual Include
-
It seems using the following scheme would cause problem while building in MS Visual Studio 2005. Can someone give any suggestion on HOW TO ACCESS MEMBER VARIABLE OF b IN a.cpp? Some preprocessor instructions would be appreciated. Thanks a lot.
// a.h class b; class a : public A { public: // Constructor, Destructor, and other members and functions. (Omitted) //... // void SomeFunction(); b* m_pb; }; // a.cpp #include "a.h" #include "b.h" void a::SomeFunction() { MEMBERVARIABLEDATATYPE Member_Variable_of_b = m_pb->MemberVariable; // This would cause compiler error } // b.h class a; class b : public B { public: // Constructor, Destructor, and other members and functions. (Omitted) //... // a* m_pa; MEMBERVARIABLEDATATYPE MemberVariable; // MEMBERVARIABLEDATATYPE is well defined, for example, int, double, etc. };
-
It seems using the following scheme would cause problem while building in MS Visual Studio 2005. Can someone give any suggestion on HOW TO ACCESS MEMBER VARIABLE OF b IN a.cpp? Some preprocessor instructions would be appreciated. Thanks a lot.
// a.h class b; class a : public A { public: // Constructor, Destructor, and other members and functions. (Omitted) //... // void SomeFunction(); b* m_pb; }; // a.cpp #include "a.h" #include "b.h" void a::SomeFunction() { MEMBERVARIABLEDATATYPE Member_Variable_of_b = m_pb->MemberVariable; // This would cause compiler error } // b.h class a; class b : public B { public: // Constructor, Destructor, and other members and functions. (Omitted) //... // a* m_pa; MEMBERVARIABLEDATATYPE MemberVariable; // MEMBERVARIABLEDATATYPE is well defined, for example, int, double, etc. };
This works fine so I'm not sure what you are doing to get an error. Output is..."Member variable of b = 22" // b.h ******************** #include "stdafx.h" class a; class b { public: int m_a; b(); virtual ~b(); }; // b.cpp ******************** b::b() : m_a(22) { } b::~b() { } // a.h ******************** #include "stdafx.h" class b; class a { public: void SomeFunction(); b* m_pb; a(); virtual ~a(); }; // a.cpp ******************** a::a() : m_pb(NULL) { m_pb=new b(); SomeFunction(); } a::~a() { if (m_pb) { delete m_pb; } } void a::SomeFunction() { if (m_pb) { int Member_Variable_of_b=m_pb->m_a; TRACE("Member variable of b = %d\n",Member_Variable_of_b); } }