code_junkie

Const Struct&

Nov 10th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. struct Widget
  2. {
  3. Widget():x(0), y(0), z(0){}
  4.  
  5. int x, y, z;
  6. };
  7.  
  8. struct WidgetHolder //Just a simple struct to hold four Widgets.
  9. {
  10. WidgetHolder(Widget a, Widget b, Widget c, Widget d): A(a), B(b), C(c), D(d){}
  11.  
  12. Widget& A;
  13. Widget& B;
  14. Widget& C;
  15. Widget& D;
  16. };
  17.  
  18. class Test //This class uses four widgets internally, and must provide access to them externally.
  19. {
  20. public:
  21. const WidgetHolder AccessWidgets() const
  22. {
  23. //This should return our four widgets, but I don't want anyone messing with them.
  24. return WidgetHolder(A, B, C, D);
  25. }
  26.  
  27. WidgetHolder AccessWidgets()
  28. {
  29. //This should return our four widgets, I don't care if they get changed.
  30. return WidgetHolder(A, B, C, D);
  31. }
  32.  
  33. private:
  34. Widget A, B, C, D;
  35. };
  36.  
  37. int main()
  38. {
  39. const Test unchangeable;
  40.  
  41. unchangeable.AccessWidgets().A.x = 1; //Why does this compile, shouldn't the Widget& be const?
  42. }
  43.  
  44. WidgetHolder(Widget a, Widget b, Widget c, Widget d): A(a), B(b), C(c), D(d){}
  45.  
  46. WidgetHolder(Widget &a, Widget &b, Widget &c, Widget &d): A(a), B(b), C(c), D(d){}
  47.  
  48. struct ConstWidgetHolder
  49. {
  50. ConstWidgetHolder(const Widget &a, const Widget &b, const Widget &c, const Widget &d): A(a), B(b), C(c), D(d){}
  51.  
  52. const Widget& A;
  53. const Widget& B;
  54. const Widget& C;
  55. const Widget& D;
  56. };
  57.  
  58. class Test
  59. {
  60. public:
  61. ConstWidgetHolder AccessWidgets() const
  62. {
  63. return ConstWidgetHolder(A, B, C, D);
  64. }
  65.  
  66. class vector {
  67. iterator begin();
  68. const_iterator begin() const;
  69.  
  70. class vector {
  71. iterator begin();
  72. const_iterator begin() const;
  73.  
  74. #include <iostream>
  75.  
  76. class Widget
  77. {
  78. int x;
  79. public:
  80. Widget(int inX) : x(inX){}
  81. ~Widget() {
  82. std::cout << "widget " << static_cast< void*>(this) << " destroyed" << std::endl;
  83. }
  84. };
  85.  
  86. struct WidgetHolder
  87. {
  88. Widget& A;
  89.  
  90. public:
  91. WidgetHolder(Widget a): A(a) {}
  92.  
  93. const Widget& a() const {
  94. std::cout << "widget " << static_cast< void*>(&A) << " used" << std::endl;
  95. return A;
  96. }
  97.  
  98. };
  99.  
  100. int main(char** argv, int argc)
  101. {
  102. Widget test(7);
  103. WidgetHolder holder(test);
  104. Widget const & test2 = holder.a();
  105.  
  106. return 0;
  107. }
  108.  
  109. #include <stdio.h>
  110.  
  111. class Test
  112. {
  113. public:
  114. Test(int v){m_v = v;}
  115. ~Test(){printf("Destruct value = %dn",m_v);}
  116.  
  117. int& GetV(){printf ("None Const returning %dn",m_v); return m_v; }
  118.  
  119. const int& GetV() const { printf("Const returning %dn",m_v); return m_v;}
  120. private:
  121. int m_v;
  122. };
  123.  
  124. void main()
  125. {
  126. // A none const object (or reference) calls the none const functions
  127. // in preference to the const
  128. Test one(10);
  129. int& x = one.GetV();
  130. // We can change the member variable via the reference
  131. x = 12;
  132.  
  133. const Test two(20);
  134. // This will call the const version
  135. two.GetV();
  136.  
  137. // So the below line will not compile
  138. // int& xx = two.GetV();
  139.  
  140. // Where as this will compile
  141. const int& xx = two.GetV();
  142.  
  143. // And then the below line will not compile
  144. // xx = 3;
  145.  
  146. }
  147.  
  148. #include <stdio.h>
  149.  
  150. class Test
  151. {
  152. public:
  153. Test(int v){m_v = v;}
  154. ~Test(){printf("Destruct value = %dn",m_v);}
  155.  
  156. int& GetV(){printf ("None Const returning %dn",m_v); return m_v; }
  157.  
  158. const int& GetV() const { printf("Const returning %dn",m_v); return m_v;}
  159. private:
  160. int m_v;
  161. };
  162.  
  163. void main()
  164. {
  165. // A none const object (or reference) calls the none const functions
  166. // in preference to the const
  167. Test one(10);
  168. int& x = one.GetV();
  169. // We can change the member variable via the reference
  170. x = 12;
  171.  
  172. const Test two(20);
  173. // This will call the const version
  174. two.GetV();
  175.  
  176. // So the below line will not compile
  177. // int& xx = two.GetV();
  178.  
  179. // Where as this will compile
  180. const int& xx = two.GetV();
  181.  
  182. // And then the below line will not compile
  183. // xx = 3;
  184.  
  185. }
  186.  
  187. #include <stdio.h>
  188.  
  189. class Test
  190. {
  191. public:
  192. Test(int v){m_v = v;}
  193. ~Test(){printf("Destruct value = %dn",m_v);}
  194.  
  195. int& GetV(){printf ("None Const returning %dn",m_v); return m_v; }
  196.  
  197. const int& GetV() const { printf("Const returning %dn",m_v); return m_v;}
  198. private:
  199. int m_v;
  200. };
  201.  
  202. void main()
  203. {
  204. // A none const object (or reference) calls the none const functions
  205. // in preference to the const
  206. Test one(10);
  207. int& x = one.GetV();
  208. // We can change the member variable via the reference
  209. x = 12;
  210.  
  211. const Test two(20);
  212. // This will call the const version
  213. two.GetV();
  214.  
  215. // So the below line will not compile
  216. // int& xx = two.GetV();
  217.  
  218. // Where as this will compile
  219. const int& xx = two.GetV();
  220.  
  221. // And then the below line will not compile
  222. // xx = 3;
  223.  
  224. }
  225.  
  226. class WidgetHolder {
  227. ...
  228.  
  229. Widget& GetA();
  230. const Widget& GetA() const;
  231. ...
  232. };
  233.  
  234. class Test {
  235. ...
  236. WigetHolder& AccessWidgets() { return m_Widgets;}
  237. const WidgetHolder&AcessWidgets() const { return m_Widgets;}
  238.  
  239. private:
  240. WidgetHolder m_Widgets;
  241. ...
  242. };
Add Comment
Please, Sign In to add comment