doc_type_erasure.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/detail/config_begin.hpp>
  11. #include <boost/container/detail/workaround.hpp>
  12. //[doc_type_erasure_MyClassHolder_h
  13. #include <boost/container/vector.hpp>
  14. //MyClassHolder.h
  15. //We don't need to include "MyClass.h"
  16. //to store vector<MyClass>
  17. class MyClass;
  18. class MyClassHolder
  19. {
  20. public:
  21. void AddNewObject(const MyClass &o);
  22. const MyClass & GetLastObject() const;
  23. private:
  24. ::boost::container::vector<MyClass> vector_;
  25. };
  26. //]
  27. //[doc_type_erasure_MyClass_h
  28. //MyClass.h
  29. class MyClass
  30. {
  31. private:
  32. int value_;
  33. public:
  34. MyClass(int val = 0) : value_(val){}
  35. friend bool operator==(const MyClass &l, const MyClass &r)
  36. { return l.value_ == r.value_; }
  37. //...
  38. };
  39. //]
  40. //[doc_type_erasure_main_cpp
  41. //Main.cpp
  42. //=#include "MyClassHolder.h"
  43. //=#include "MyClass.h"
  44. #include <cassert>
  45. int main()
  46. {
  47. MyClass mc(7);
  48. MyClassHolder myclassholder;
  49. myclassholder.AddNewObject(mc);
  50. return myclassholder.GetLastObject() == mc ? 0 : 1;
  51. }
  52. //]
  53. //[doc_type_erasure_MyClassHolder_cpp
  54. //MyClassHolder.cpp
  55. //=#include "MyClassHolder.h"
  56. //In the implementation MyClass must be a complete
  57. //type so we include the appropriate header
  58. //=#include "MyClass.h"
  59. void MyClassHolder::AddNewObject(const MyClass &o)
  60. { vector_.push_back(o); }
  61. const MyClass & MyClassHolder::GetLastObject() const
  62. { return vector_.back(); }
  63. //]
  64. #include <boost/container/detail/config_end.hpp>