code_junkie

Alternative to forward declarations when you don't want to #include

Nov 12th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. //-----------------------
  2. // foo.h
  3. //-----------------------
  4. class foo
  5. {
  6. foo();
  7. ~foo();
  8. };
  9.  
  10.  
  11. //-----------------------
  12. // bar.h
  13. //-----------------------
  14.  
  15. class foo; // forward declaration
  16.  
  17. class bar
  18. {
  19. bar();
  20. ~bar();
  21.  
  22. foo* foo_pointer;
  23. };
  24.  
  25. //-----------------------
  26. // foo.h
  27. //-----------------------
  28. class foo
  29. {
  30. foo();
  31. ~foo();
  32. };
  33.  
  34.  
  35. //-----------------------
  36. // bar.h
  37. //-----------------------
  38.  
  39. class foo; // Not enough given the way we declare "foo_object"..
  40. #include "foo.h" // ..instead this is required
  41.  
  42. class bar
  43. {
  44. bar();
  45. ~bar();
  46.  
  47. foo foo_object;
  48. };
  49.  
  50. //-----------------------
  51. // bar.h
  52. //-----------------------
  53.  
  54. #include <memory>
  55. class foo; // Not enough given the way we declare "foo_object"..
  56.  
  57. class bar
  58. {
  59. public:
  60. bar();
  61. ~bar();
  62.  
  63. foo &foo_object() { return *foo_ptr; }
  64. const foo &foo_object() const { return *foo_ptr; }
  65.  
  66. private:
  67. auto_ptr<foo> foo_ptr;
  68. };
  69.  
  70. #include <iostream>
  71. using namespace std;
  72.  
  73. class Foo;
  74.  
  75. template <typename T>
  76. class DefaultConstuctorPtr
  77. {
  78. T *ptr;
  79. void operator =(const DefaultConstuctorPtr &);
  80. DefaultConstuctorPtr(const DefaultConstuctorPtr &);
  81.  
  82. public:
  83. DefaultConstuctorPtr() : ptr(new T()) {}
  84. ~DefaultConstuctorPtr() { delete ptr; }
  85.  
  86. T *operator *() { return ptr; }
  87. const T *operator *() const { return ptr; }
  88. };
  89.  
  90. class Bar
  91. {
  92. DefaultConstuctorPtr<Foo> foo_ptr;
  93. public:
  94. Bar() {} // The compiler should really need Foo() to be defined here?
  95. };
  96.  
  97. class Foo
  98. {
  99. public:
  100. Foo () { cout << "Constructing foo"; }
  101. };
  102.  
  103. int main()
  104. {
  105. Bar bar;
  106. }
  107.  
  108. template<typename Type>
  109. struct member {
  110. boost::shared_ptr<Type> ptr;
  111. member(): ptr(new Type) { }
  112. };
  113.  
  114. struct foo;
  115. struct bar {
  116. bar();
  117. ~bar();
  118.  
  119. // automatic management for m
  120. member<foo> m;
  121. };
  122.  
  123. // bar.h
  124. class foo;
  125.  
  126. class bar {
  127. foo& foo_;
  128.  
  129. public:
  130. bar();
  131. ~bar();
  132. };
  133.  
  134. // bar.cc
  135. bar::bar() : foo_(*new foo)
  136. {
  137. // ...
  138. }
  139.  
  140. bar::~bar()
  141. {
  142. // ...
  143. delete &foo_;
  144. }
  145.  
  146. template<class T>
  147. class myAuto
  148. {
  149. private:
  150. T * obj;
  151.  
  152. public:
  153. myAuto() : obj(new T) { }
  154. ~myAuto() { delete obj; }
  155. T& object() { return *obj; }
  156. T* operator ->() { return obj; }
  157. };
  158.  
  159. // foo.h:
  160. class foo
  161. {
  162. public:
  163. foo();
  164. ~foo();
  165. void some_foo_func();
  166. };
  167.  
  168. //bar.h:
  169. class foo;
  170. class bar
  171. {
  172. public:
  173. bar();
  174. ~bar();
  175. myAuto<foo> foo_object;
  176. };
  177.  
  178. //main.cc:
  179. #include "foo.h"
  180. #include "bar.h"
  181.  
  182. int main()
  183. {
  184. bar a_bar;
  185.  
  186. a_bar.foo_object->some_foo_func();
  187.  
  188. return 0;
  189. }
  190.  
  191. //-----------------------
  192. // foo.h
  193. //-----------------------
  194. class foo
  195. {
  196. foo();
  197. ~foo();
  198. };
  199.  
  200.  
  201. //-----------------------
  202. // bar.h
  203. //-----------------------
  204.  
  205. class foo;
  206.  
  207. class bar
  208. {
  209. private:
  210. struct impl;
  211. boost::shared_ptr<impl> impl_;
  212. public:
  213. bar();
  214.  
  215. const foo& get_foo() const;
  216. };
  217.  
  218. //-----------------------
  219. // bar.cpp
  220. //-----------------------
  221. #include "bar.h"
  222. #include "foo.h"
  223.  
  224. struct bar::impl
  225. {
  226. foo foo_object;
  227. ...
  228. }
  229.  
  230. bar::bar() :
  231. impl_(new impl)
  232. {
  233. }
  234.  
  235. const foo& bar::get_foo() const
  236. {
  237. return impl_->foo_object;
  238. }
Add Comment
Please, Sign In to add comment