Bonjour,
Comme dit dans le titre, j'ai un petit problème de conception.
Enfin c'est plus une interrogation qu'un problème, mais bon...
En fait, j'ai un couple de valeurs, avec une sémantique précise.
Pour certains cas, j'aimerais augmenter ce couple avec une troisième valeur.
La sémantique du couple restant la même, mais avec la nouvelle valeur le tout devient un triplet à part entière.
D'où la seconde partie de la question dans le titre.
Alors à votre avis, 1, 2 ou 3 ?
Code Couple : 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 class couple { public: typedef ... first_type; typedef ... second_type; private: first_type m_first; second_type m_second; public: couple() = default; couple(first_type const& e1, second_type const& e2) : m_first(e1), m_second(e2) {} first_type const& get_first() const { return m_first; } second_type const& get_second() const { return m_second; } void get(first_type& e1, second_type& e2) const { e1 = m_first; e2 = m_second; } void reset() { m_first = first_type(); m_second = second_type(); } void set(first_type const& e1, second_type const& e2) { m_first = e1; m_second = e2; } }; // class couple bool operator == (couple const& lhs, couple const& rhs) { return ( (lhs.get_first() == rhs.get_first()) && (lhs.get_second() == rhs.get_second()) ); } bool operator < (couple const& lhs, couple const& rhs) { return ( (lhs.get_first() < rhs.get_first()) || ( (lhs.get_first() == rhs.get_first()) && (lhs.get_second() < rhs.get_second()) )); }
Code Héritage : 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 class triplet : private couple { public: typedef couple::first_type first_type; typedef couple::second_type second_type; typedef ... third_type; private: third_type m_third; public: triplet() = default; triplet(first_type const& e1, second_type const& e2, third_type const& e3) : couple(e1, e2), m_third(e3) {} using couple::get_first; using couple::get_second; third_type const& get_third() const { return m_third; } void get(first_type& e1, second_type& e2, third_type& e3) const { couple::get(e1, e2); e3 = m_third; } void reset() { couple::reset(); m_third = third_type(); } void set(first_type const& e1, second_type const& e2, third_type const& e3) { couple::set(e1, e2); m_third = e3; } }; // class triplet bool operator == (triplet const& lhs, triplet const& rhs) { return ( (lhs.get_first() == rhs.get_first()) && (lhs.get_second() == rhs.get_second()) && (lhs.get_third() == rhs.get_third()) ); } bool operator < (triplet const& lhs, triplet const& rhs) { if (lhs.get_first() < rhs.get_first()) return true; if (rhs.get_first() == lhs.get_first()) { if (lhs.get_second() < rhs.get_second()) return true; return ( (lhs.get_second() == rhs.get_second()) && (lhs.get_third() < rhs.get_third()) ); } return false; }
Code Composition : 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 class triplet { public: typedef couple::first_type first_type; typedef couple::second_type second_type; typedef ... third_type; private: couple m_couple; third_type m_third; public: triplet() = default; triplet(first_type const& e1, second_type const& e2, third_type const& e3) : m_couple(e1, e2), m_third(e3) {} first_type const& get_first() const { return m_couple.get_first(); } second_type const& get_second() const { return m_couple.get_second(); } third_type const& get_third() const { return m_third; } void get(first_type& e1, second_type& e2, third_type& e3) const { m_couple.get(e1, e2); e3 = m_third; } void reset() { m_couple.reset(); m_third = third_type(); } void set(first_type const& e1, second_type const& e2, third_type const& e3) { m_couple.set(e1, e2); m_third = e3; } }; // class triplet bool operator == (triplet const& lhs, triplet const& rhs) { return ( (lhs.get_first() == rhs.get_first()) && (lhs.get_second() == rhs.get_second()) && (lhs.get_third() == rhs.get_third()) ); } bool operator < (triplet const& lhs, triplet const& rhs) { if (lhs.get_first() < rhs.get_first()) return true; if (rhs.get_first() == lhs.get_first()) { if (lhs.get_second() < rhs.get_second()) return true; return ( (lhs.get_second() == rhs.get_second()) && (lhs.get_third() < rhs.get_third()) ); } return false; }
Code Séparation : 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 class triplet { public: typedef ... first_type; typedef ... second_type; typedef ... third_type; private: first_type m_first; second_type m_second; third_type m_third; public: triplet() = default; triplet(first_type const& e1, second_type const& e2, third_type const& e3) : m_first(e1), m_second(e2), m_third(e3) {} first_type const& get_first() const { return m_first; } second_type const& get_second() const { return m_second; } third_type const& get_third() const { return m_third; } void get(first_type& e1, second_type& e2, third_type& e3) const { e1 = m_first; e2 = m_second; e3 = m_third; } void reset() { m_first = first_type(); m_second = second_type(); m_third = third_type(); } void set(first_type const& e1, second_type const& e2, third_type const& e3) { m_first = e1; m_second = e2; m_third = e3; } }; // class triplet bool operator == (triplet const& lhs, triplet const& rhs) { return ( (lhs.get_first() == rhs.get_first()) && (lhs.get_second() == rhs.get_second()) && (lhs.get_third() == rhs.get_third()) ); } bool operator < (triplet const& lhs, triplet const& rhs) { if (lhs.get_first() < rhs.get_first()) return true; if (rhs.get_first() == lhs.get_first()) { if (lhs.get_second() < rhs.get_second()) return true; return ( (lhs.get_second() == rhs.get_second()) && (lhs.get_third() < rhs.get_third()) ); } return false; }
Il me semble qu'il vaudrait mieux utiliser le troisième code, mais je me demandais s'il n'y avait pas une possibilité de réutiliser le code de la classe couple tout en restant cohérent/correct.
Partager