bonjour j'ai le code suivant
et j'essai de la transformer en template
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 class ThreadList { public: class iterator{ private: ThreadList *source; int index = 0; public: iterator(ThreadList *source) : source(source){ try{ source->iteratorAdded(); } catch (...){ } }; iterator(iterator &iter) : iterator(iter.source) { }; ~iterator(){ try{ source->iteratorDeleted(); } catch (...){ } } IThread* operator*() { try{ return source->list[index]; } catch (...){ return NULL; } } IThread* operator++() { IThread* i = source->list[index]; index ++; return i; } bool operator!=(const int& rhs) { return !(index >= source->list.size() && rhs == 0); } }; private: bool _AutoStart; int _Iterator; void iteratorAdded() { _Iterator++; cout << "iterator added = " << _Iterator << endl; } void iteratorDeleted(){ _Iterator--; cout << "iterator deleted = " << _Iterator << endl; if (_Iterator == 0){ clearList(); } }; void clearList(){ cout << "Clear list" << endl; }; public: ThreadList(bool autoStart = true) { _AutoStart = autoStart; _Iterator = 0; } ~ThreadList() { for (vector<IThread*>::iterator it = list.begin(); it < list.end(); it++) { delete (*it); } } void add(IThread* thread) { if (_AutoStart) thread->start(); list.push_back(thread); } iterator begin(){ iterator it(this); return it; } private: vector<IThread*> list; };
or j'ai un problème sur la fonction (il ne veut pas compiler)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 template<class T> class ClearableList { public: class iterator{ private: T source; int index = 0; public: iterator(T source) : source(source){ try{ source.iteratorAdded(); } catch (...){ } }; iterator(iterator &iter) : iterator(iter.source) { }; ~iterator(){ try{ source->iteratorDeleted(); } catch (...){ } } T operator*() { try{ return source.list[index]; } catch (...){ return NULL; } } T operator++() { IThread* i = source->list[index]; index++; return i; } bool operator!=(const int& rhs) { return !(index >= source.list.size() && rhs == 0); } }; private: int _Iterator; void iteratorAdded() { _Iterator++; cout << "iterator added = " << _Iterator << endl; } void iteratorDeleted(){ _Iterator--; cout << "iterator deleted = " << _Iterator << endl; if (_Iterator == 0){ clearList(); } }; void clearList(){ cout << "Clear list" << endl; }; public: ClearableList() { _Iterator = 0; } ~ClearableList() { for (vector<T>::iterator it = list.begin(); it < list.end(); it++) { delete (*it); } } void add(T value) { list.push_back(value); } iterator begin(){ iterator it(this); return it; } private: vector<T> list; };
vous auriez une idée ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 iterator begin(){ iterator it(this); return it; }
Partager