Advertisement
Guest User

Untitled

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