Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. class CloneNotSupported {
  2. };
  3.  
  4.  
  5. class Object {
  6.  
  7. public:
  8.  
  9. virtual ~Object() {}
  10.  
  11. virtual Object *clone() const {
  12.  
  13. throw CloneNotSupported();
  14.  
  15. }
  16.  
  17. };
  18.  
  19.  
  20. class List : public Object {
  21.  
  22. friend class Iterator;
  23.  
  24. class ListElement {
  25.  
  26. public:
  27.  
  28. ListElement *next;
  29.  
  30. Object *object;
  31.  
  32. };
  33.  
  34. ListElement *start;
  35.  
  36. ListElement *end;
  37.  
  38.  
  39. public:
  40.  
  41. List() { start = end = 0; }
  42.  
  43. ~List() { free(); }
  44.  
  45. List(const List &other) { copy(other); }
  46.  
  47. List &operator=(const List &other) {
  48.  
  49. if (&other != this) {
  50.  
  51. free();
  52.  
  53. copy(other);
  54.  
  55. }
  56.  
  57. return *this;
  58.  
  59. }
  60.  
  61.  
  62. void pushBack(Object *object) {
  63.  
  64. ListElement *le = new ListElement();
  65.  
  66. le->next = 0;
  67.  
  68. le->object = object;
  69.  
  70. if (end)end->next = le;
  71.  
  72. end = le;
  73.  
  74. if (!start)start = end;
  75.  
  76. }
  77.  
  78. void pushFront(Object *object) {
  79.  
  80. ListElement *le = new ListElement();
  81.  
  82. le->object = object;
  83.  
  84. le->next = start;
  85.  
  86. start = le;
  87.  
  88. if (!end)end = start;
  89.  
  90. }
  91.  
  92. protected:
  93.  
  94. void free() {
  95.  
  96. while (start) {
  97.  
  98. ListElement *tmp = start;
  99.  
  100. start = start->next;
  101.  
  102. delete tmp->object;
  103.  
  104. delete tmp;
  105.  
  106. }
  107.  
  108. start = end = 0;
  109.  
  110. }
  111.  
  112. void copy(const List &other) {
  113.  
  114. start = end = 0;
  115.  
  116. for (ListElement *i = other.start; i; i = i->next) {
  117.  
  118. pushBack(i->object->clone());
  119.  
  120. }
  121.  
  122. }
  123.  
  124. };
  125.  
  126.  
  127. class Iterator {
  128.  
  129. const List &list;
  130.  
  131. List::ListElement *current;
  132.  
  133. public:
  134.  
  135. Iterator(const List &l) : list(l), current(l.start) {}
  136.  
  137. Iterator &operator++() {
  138.  
  139. if (current)current = current->next;
  140.  
  141. return *this;
  142.  
  143. }
  144.  
  145. Iterator &operator++(int) {
  146.  
  147. if (current->next)current = current->next; //?
  148.  
  149. return *this;
  150.  
  151. }
  152.  
  153. operator bool() const { return current != 0; }
  154.  
  155. Object *get() const { return current->object; }
  156.  
  157. };
  158.  
  159.  
  160. class Int : public Object {
  161.  
  162. public:
  163.  
  164. int value;
  165.  
  166. Int(int v = 0) { value = v; }
  167.  
  168. void dump() const { std::cout << value << std::endl; }
  169.  
  170. // Object*clone()const{return new Int(*this);}
  171.  
  172. };
  173.  
  174.  
  175. int main(int argc, char *argv[]) {
  176.  
  177. // stwórz listę, dodaj elementy, wypisz
  178.  
  179. List list;
  180.  
  181. for (int i = 0; i < 100; i++) {
  182.  
  183. list.pushFront(new Int(2 * i));
  184.  
  185. list.pushBack(new Int(2 * i + 1));
  186.  
  187. }
  188.  
  189. for (Iterator i(list); i; i++) {
  190.  
  191. Int *pi = dynamic_cast<Int *>(i.get());
  192.  
  193. if (pi)pi->dump();
  194.  
  195. }
  196.  
  197. // utwórz kopię listy i wypisz zawartość
  198.  
  199. try {
  200.  
  201. List list2(list);
  202.  
  203. for (Iterator i(list2); i; i++) {
  204.  
  205. Int *pi = dynamic_cast<Int *>(i.get());
  206.  
  207. if (pi)pi->dump();
  208.  
  209. }
  210.  
  211. }
  212.  
  213. catch (CloneNotSupported) {
  214.  
  215. std::cout << "Clone not supported" << std::endl;
  216.  
  217. }
  218.  
  219.  
  220. return 0;
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement