]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | //[doc_type_erasure_MyClassHolder_h | |
12 | #include <boost/container/vector.hpp> | |
13 | ||
14 | //MyClassHolder.h | |
15 | ||
16 | //We don't need to include "MyClass.h" | |
17 | //to store vector<MyClass> | |
18 | class MyClass; | |
19 | ||
20 | class MyClassHolder | |
21 | { | |
22 | public: | |
23 | ||
24 | void AddNewObject(const MyClass &o); | |
25 | const MyClass & GetLastObject() const; | |
26 | ||
27 | private: | |
28 | ::boost::container::vector<MyClass> vector_; | |
29 | }; | |
30 | ||
31 | //] | |
32 | ||
33 | //[doc_type_erasure_MyClass_h | |
34 | ||
35 | //MyClass.h | |
36 | ||
37 | class MyClass | |
38 | { | |
39 | private: | |
40 | int value_; | |
41 | ||
42 | public: | |
43 | MyClass(int val = 0) : value_(val){} | |
44 | ||
45 | friend bool operator==(const MyClass &l, const MyClass &r) | |
46 | { return l.value_ == r.value_; } | |
47 | //... | |
48 | }; | |
49 | ||
50 | //] | |
51 | ||
52 | ||
53 | ||
54 | //[doc_type_erasure_main_cpp | |
55 | ||
56 | //Main.cpp | |
57 | ||
58 | //=#include "MyClassHolder.h" | |
59 | //=#include "MyClass.h" | |
60 | ||
61 | #include <cassert> | |
62 | ||
63 | int main() | |
64 | { | |
65 | MyClass mc(7); | |
66 | MyClassHolder myclassholder; | |
67 | myclassholder.AddNewObject(mc); | |
68 | return myclassholder.GetLastObject() == mc ? 0 : 1; | |
69 | } | |
70 | //] | |
71 | ||
72 | //[doc_type_erasure_MyClassHolder_cpp | |
73 | ||
74 | //MyClassHolder.cpp | |
75 | ||
76 | //=#include "MyClassHolder.h" | |
77 | ||
78 | //In the implementation MyClass must be a complete | |
79 | //type so we include the appropriate header | |
80 | //=#include "MyClass.h" | |
81 | ||
82 | void MyClassHolder::AddNewObject(const MyClass &o) | |
83 | { vector_.push_back(o); } | |
84 | ||
85 | const MyClass & MyClassHolder::GetLastObject() const | |
86 | { return vector_.back(); } | |
87 | ||
88 | //] |