1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include <iostream>
#include <limits>
class Mother
{
public :
Mother() {}
virtual ~Mother() {}
virtual void doSomething() = 0;
Mother* next;
Mother* previous;
int numero;
};
class List
{
public:
List() :pos(-1), current(NULL) {}
~List() {}
void insert(Mother * elem);
Mother* operator[] (int i);
static int numero;
private:
Mother * current;
int pos;
};
int List::numero = 0;
Mother* List::operator [](int i)
{
Mother * obj = current;
int pos_cur = pos;
while ( i != pos_cur )
{
if (i > pos)
{
obj = obj->next;
pos_cur++;
}
if (i < pos)
{
obj = obj->previous;
pos_cur--;
}
}
return obj;
}
void List::insert(Mother* elem)
{
numero++;
if (current == NULL)
{
current = elem;
current->previous = NULL;
current->numero = List::numero;
}
else
{
elem->previous = current;
current->next = elem;
current = elem; //on met à jour
current->numero = List::numero;
}
pos++;
}
class A : public Mother
{
public:
A() {}
virtual void doSomething() { std::cout << "Je suis A : " << numero << std::endl; }
};
class B : public Mother
{
public:
B() {}
virtual void doSomething() { std::cout << "Je suis B : " << numero << std::endl;}
};
int main()
{
List list;
A* a = new A();
B* b = new B();
B* b2 = new B();
A* a2 = new A();
list.insert(a);
list.insert(b);
list.insert(a2);
list.insert(b2);
list[0]->doSomething();
list[1]->doSomething();
list[2]->doSomething();
list[3]->doSomething();
delete a;
delete b;
delete a2;
delete b2;
std::cout << "Appuyez sur entrée pour continuer...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
} |
Partager