code_junkie

C++ Forward Declaration Problem when calling Method

Nov 13th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #ifndef A_H_
  2. #define A_H_
  3.  
  4. #include "B.h"
  5.  
  6. class A
  7. {
  8. private:
  9. B b;
  10.  
  11. public:
  12. A() : b(*this) {}
  13.  
  14. void bar() {}
  15. };
  16.  
  17. #endif /*A_H_*/
  18.  
  19. #ifndef B_H_
  20. #define B_H_
  21.  
  22. #include "A.h"
  23.  
  24. class A;
  25.  
  26. class B
  27. {
  28. private:
  29. A& a;
  30.  
  31. public:
  32. B(A& a) : a(a) {}
  33.  
  34. void foo() { /*a.bar();*/ } //doesn't compile
  35. };
  36.  
  37. #endif /*B_H_*/
  38.  
  39. #include "A.h"
  40.  
  41. int main()
  42. {
  43. A a;
  44.  
  45. return 0;
  46. }
  47.  
  48. #ifndef B_H_
  49. #define B_H_
  50.  
  51. // don't include A.h here!
  52.  
  53. class A;
  54.  
  55. class B
  56. {
  57. private:
  58. A& a;
  59.  
  60. public:
  61. B(A& a) : a(a) {}
  62.  
  63. void foo();
  64. };
  65.  
  66. #endif /*B_H_*/
  67.  
  68. #include "B.h"
  69. #include "A.h"
  70.  
  71. void B::foo() { a.bar(); } // now you're ok
  72.  
  73. // #include "A.h" ==>
  74. #define A_H_
  75.  
  76. // #include "B.h" ==>
  77. #define B_H_
  78.  
  79. // #include "A.h" ==> nothing happens! (since A_H_ is already defined)
  80.  
  81. class A;
  82.  
  83. class B {
  84. private:
  85. A& a;
  86.  
  87. public:
  88. B(A& a) : a(a) {}
  89.  
  90. void foo() { a.bar(); } // <-- what the heck is A here?
  91. // it's not defined until below
  92. };
  93.  
  94. class A {
  95. private:
  96. B b;
  97.  
  98. public:
  99. A() : b(*this) {}
  100.  
  101. void bar() {}
  102. };
  103.  
  104. int main() {
  105. A a;
  106. return 0;
  107. }
  108.  
  109. // result from #include "A.h"
  110.  
  111. #ifndef A_H_
  112. #define A_H_
  113.  
  114. // Now, #include "B.h" in A.h will get you the following
  115. #ifndef B_H_
  116. #define B_H_
  117.  
  118. // here you include "A.h" again, but now it has no effect
  119. // since A_H_ is already defined
  120.  
  121. class A;
  122.  
  123. class B
  124. {
  125. private:
  126. A& a;
  127. public:
  128. B(A& a) : a(a) {}
  129. // Oops, you want to use a.bar() but it is not defined yet
  130. void foo() { /*a.bar();*/ }
  131. };
  132.  
  133. #endif /*B_H_*/
  134.  
  135. class A
  136. {
  137. private:
  138. B b;
  139. public:
  140. A() : b(*this) {}
  141. void bar() {}
  142. };
  143. #endif /*A_H_*/
  144.  
  145. // now this is your main function
  146. int main()
  147. {
  148. A a;
  149. return 0;
  150. }
  151.  
  152. class IAnalyzer
  153. {
  154. public:
  155. virtual void Analyze () = 0;
  156. };
  157.  
  158. class DataAnalyzer : public IAnalyzer
  159. {
  160. public:
  161. DataAnalyzer (Data* data);
  162.  
  163. virtual void Analyze (); // defined in DataAnalyzer.cpp
  164. };
  165.  
  166. class Data
  167. {
  168. public:
  169. Data ();
  170.  
  171. IAnalyzer* Analyzer;
  172. };
  173.  
  174. void main ()
  175. {
  176. Data* data = new Data ();
  177. data->Analyzer = new DataAnalyzer (data);
  178. }
  179.  
  180. #ifndef B_H_
  181. #define B_H_
  182.  
  183. // Forward declaration of A.
  184. class A;
  185.  
  186. class B
  187. {
  188. private:
  189. A& a;
  190.  
  191. public:
  192. B(A& a) : a(a) {}
  193.  
  194. void foo();
  195. };
  196.  
  197. // Include definition of A after definition of B.
  198. #include "A.h"
  199.  
  200. inline void B::foo()
  201. {
  202. a.bar();
  203. }
  204.  
  205. #endif /*B_H_*/
  206.  
  207. // Include definition of B before definition of A.
  208. // This must be done before the ifndef A_H_ include sentry,
  209. // otherwise A.h cannot be included without including A.h first.
  210. #include "B.h"
  211.  
  212. #ifndef A_H_
  213. #define A_H_
  214.  
  215. class A
  216. {
  217. private:
  218. B b;
  219.  
  220. public:
  221. A() : b(*this) {}
  222.  
  223. void bar() {}
  224. };
  225.  
  226. #endif /*A_H_*/
Add Comment
Please, Sign In to add comment