| 1 | #include <boost/ptr_container/ptr_list.hpp>
|
|---|
| 2 | #include <iostream>
|
|---|
| 3 |
|
|---|
| 4 | using namespace std;
|
|---|
| 5 |
|
|---|
| 6 | class BASE
|
|---|
| 7 | {
|
|---|
| 8 | public:
|
|---|
| 9 | virtual int print() const = 0;
|
|---|
| 10 | };
|
|---|
| 11 |
|
|---|
| 12 | class CHILD: public BASE
|
|---|
| 13 | {
|
|---|
| 14 | public:
|
|---|
| 15 | CHILD(int a) :
|
|---|
| 16 | i(a)
|
|---|
| 17 | {
|
|---|
| 18 | }
|
|---|
| 19 | int i;
|
|---|
| 20 |
|
|---|
| 21 | virtual int print() const
|
|---|
| 22 | {
|
|---|
| 23 | return i;
|
|---|
| 24 | }
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | int main()
|
|---|
| 28 | {
|
|---|
| 29 | // removing const causes it to compile
|
|---|
| 30 | boost::ptr_list<const BASE> intList;
|
|---|
| 31 | boost::ptr_list<const BASE>::iterator iterIntList;
|
|---|
| 32 |
|
|---|
| 33 | BASE *a = new CHILD(1);
|
|---|
| 34 | BASE *b = new CHILD(2);
|
|---|
| 35 | BASE *c = new CHILD(3);
|
|---|
| 36 |
|
|---|
| 37 | intList.push_back(a);
|
|---|
| 38 | intList.push_back(b);
|
|---|
| 39 | intList.push_back(c);
|
|---|
| 40 |
|
|---|
| 41 | for (iterIntList = intList.begin(); iterIntList != intList.end(); iterIntList++)
|
|---|
| 42 | {
|
|---|
| 43 | cout << iterIntList->print() << endl;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | intList.clear(); // All pointers held in list are deleted.
|
|---|
| 47 |
|
|---|
| 48 | }
|
|---|