Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef A_H_
- #define A_H_
- #include "B.h"
- class A
- {
- private:
- B b;
- public:
- A() : b(*this) {}
- void bar() {}
- };
- #endif /*A_H_*/
- #ifndef B_H_
- #define B_H_
- #include "A.h"
- class A;
- class B
- {
- private:
- A& a;
- public:
- B(A& a) : a(a) {}
- void foo() { /*a.bar();*/ } //doesn't compile
- };
- #endif /*B_H_*/
- #include "A.h"
- int main()
- {
- A a;
- return 0;
- }
- #ifndef B_H_
- #define B_H_
- // don't include A.h here!
- class A;
- class B
- {
- private:
- A& a;
- public:
- B(A& a) : a(a) {}
- void foo();
- };
- #endif /*B_H_*/
- #include "B.h"
- #include "A.h"
- void B::foo() { a.bar(); } // now you're ok
- // #include "A.h" ==>
- #define A_H_
- // #include "B.h" ==>
- #define B_H_
- // #include "A.h" ==> nothing happens! (since A_H_ is already defined)
- class A;
- class B {
- private:
- A& a;
- public:
- B(A& a) : a(a) {}
- void foo() { a.bar(); } // <-- what the heck is A here?
- // it's not defined until below
- };
- class A {
- private:
- B b;
- public:
- A() : b(*this) {}
- void bar() {}
- };
- int main() {
- A a;
- return 0;
- }
- // result from #include "A.h"
- #ifndef A_H_
- #define A_H_
- // Now, #include "B.h" in A.h will get you the following
- #ifndef B_H_
- #define B_H_
- // here you include "A.h" again, but now it has no effect
- // since A_H_ is already defined
- class A;
- class B
- {
- private:
- A& a;
- public:
- B(A& a) : a(a) {}
- // Oops, you want to use a.bar() but it is not defined yet
- void foo() { /*a.bar();*/ }
- };
- #endif /*B_H_*/
- class A
- {
- private:
- B b;
- public:
- A() : b(*this) {}
- void bar() {}
- };
- #endif /*A_H_*/
- // now this is your main function
- int main()
- {
- A a;
- return 0;
- }
- class IAnalyzer
- {
- public:
- virtual void Analyze () = 0;
- };
- class DataAnalyzer : public IAnalyzer
- {
- public:
- DataAnalyzer (Data* data);
- virtual void Analyze (); // defined in DataAnalyzer.cpp
- };
- class Data
- {
- public:
- Data ();
- IAnalyzer* Analyzer;
- };
- void main ()
- {
- Data* data = new Data ();
- data->Analyzer = new DataAnalyzer (data);
- }
- #ifndef B_H_
- #define B_H_
- // Forward declaration of A.
- class A;
- class B
- {
- private:
- A& a;
- public:
- B(A& a) : a(a) {}
- void foo();
- };
- // Include definition of A after definition of B.
- #include "A.h"
- inline void B::foo()
- {
- a.bar();
- }
- #endif /*B_H_*/
- // Include definition of B before definition of A.
- // This must be done before the ifndef A_H_ include sentry,
- // otherwise A.h cannot be included without including A.h first.
- #include "B.h"
- #ifndef A_H_
- #define A_H_
- class A
- {
- private:
- B b;
- public:
- A() : b(*this) {}
- void bar() {}
- };
- #endif /*A_H_*/
Add Comment
Please, Sign In to add comment