35 / 35 / 7
Регистрация: 02.05.2013
Сообщений: 109
|
|
1
|
Непонятный вывод: постфиксные и префиксные инкремент/декремент
27.08.2013, 12:29. Показов 876. Ответов 2
Под спойлером весь который можно просто скопипастить и запустить в онлайн компиляторе http://www.compileonline.com/c... online.php
Кликните здесь для просмотра всего текста
C++ | 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
| #include <iostream>
using namespace std;
//-------------------------------------------------------
// class Int
class Int {
public:
// constructors
Int(int n = 0) : member(n) { }
Int(const Int& i) : member(i.member) { }
Int& operator=(const Int& i);
// access
int data() const { return member; }
// modyfication
void set_member(int n) { member = n; }
// operators
const Int& operator+() {return *this; } // unary+ operator
const Int operator-() {return Int(-this->member); } //unary- operator
const Int& operator++() {++member; return *this; } // prefix ++ increment operator
const Int operator++(int) {Int Old_value(*this); ++member; return Old_value; } // postfix ++ increment operator
const Int& operator--() {--member; return *this; } // prefix ++ increment operator
const Int operator--(int) {Int Old_value(*this); --member; return Old_value; } // postfix ++ increment operator
private:
int member;
};
//-------------------
// members
// assignment operator
Int& Int::operator=(const Int& i) {
if(this==&i) return *this;
member = i.member;
return *this;
}
//-------------------
// not members
// binary operator+
const Int operator+(const Int& left, const Int& right) {
return Int(left.data()+right.data());
}
//-------------------
// binary operator-
const Int operator-(const Int& left, const Int& right) {
return Int(left.data()-right.data());
}
//-------------------
// binary operator*
const Int operator*(const Int& left, const Int& right) {
return Int(left.data()*right.data());
}
//-------------------
// binary operator/
const Int operator/(const Int& left, const Int& right) {
return Int(left.data()/right.data());
}
//-------------------
// binary operator%
const Int operator%(const Int& left, const Int& right) {
return Int(left.data()%right.data());
}
//-------------------
// operator>>
istream& operator>>(istream& is, Int& i) {
int n;
is >> n;
if (is) i.set_member(n);
return is;
}
//-------------------
// operator<<
ostream& operator<<(ostream& os, const Int& i) {
os << i.data();
return os;
}
//-------------------------------------------------------
// class template<class T> Number
template<class T>
class Number {
public:
// constructors
Number(T n = 0) : member(n) { }
Number(const Number& i) : member(i.member) { }
Number& operator=(const Number& n);
// access
T data() const { return member; }
// modyfication
void set_member(T n) { member = n; }
// operators
const Number& operator+() {return *this; } // unary+ operator
const Number operator-() {return Number(-member); } //unary- operator
const Number& operator++() {member++; return *this; } // prefix ++ increment operator
const Number operator++(int) {Number Old_value(*this); member++; return Old_value; } // postfix ++ increment operator
const Number& operator--() {member--; return *this; } // prefix ++ increment operator
const Number operator--(int) {Number Old_value(*this); member--; return Old_value; } // postfix ++ increment operator
private:
T member;
};
//-------------------
// members
// assignment operator
template<class T>
Number<T>& Number<T>::operator=(const Number<T>& n) {
if(this==&n) return *this;
member = n.member;
return *this;
}
//-------------------
// not members
// binary operator+
template<class T>
const Number<T> operator+(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()+right.data());
}
//-------------------
// binary operator-
template<class T>
const Number<T> operator-(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()-right.data());
}
//-------------------
// binary operator*
template<class T>
const Number<T> operator*(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()*right.data());
}
//-------------------
// binary operator/
template<class T>
const Number<T> operator/(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()/right.data());
}
//-------------------
// binary operator%
template<class T>
const Number<T> operator%(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()%right.data());
}
//-------------------
// operator>>
template<class T>
istream& operator>>(istream& is, Number<T>& i) {
T n;
is >> n;
if (is) i.set_member(n);
return is;
}
//-------------------
// operator<<
template<class T>
ostream& operator<<(ostream& os, const Number<T>& i) {
os << i.data();
return os;
}
int main()
{
Int i(10);
cout << -i << "; and value is " << i << endl;
cout << --i << "; and value is " << i << endl;
cout << +i << "; and value is " << i << endl;
cout << ++i << "; and value is " << i << endl;
cout << "postfix operators" << endl;
cout << i++ << "; and value is " << i << endl;
cout << i-- << "; and value is " << i << endl;
cout << endl << "-----------------" << endl;
Number<int> n1(100);
Number<int> n2(5);
cout << n1 << " % " << n2 << " == " <<n1%n2 << endl;
cout << n1 << " / " << n2 << " == " <<n1/n2 << endl;
cout << n1 << " * " << n2 << " == " <<n1*n2 << endl;
cout << n1 << " - " << n2 << " == " <<n1-n2 << endl;
cout << n1 << " + " << n2 << " == " <<n1+n2 << endl;
cout << "n1 == 100; correct work:" << endl;
cout << n1;
cout << " prefix-- == " << --n1 << endl;
cout << n1;
cout << " postfix++ == " << n1++ << "; and n1 value == " << n1 << endl;
cout << "----------------" << endl;
cout << "n1 == 100; incorrect work:" << endl;
cout << n1 << " prefix-- == " << --n1 << endl;
cout << n1 << " postfix++ == " << n1++ << "; and n1 value == " << n1 << endl;
return 0;
} |
|
Есть два класса.
Первый:
C++ | 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
| //-------------------------------------------------------
// class Int
class Int {
public:
// constructors
Int(int n = 0) : member(n) { }
Int(const Int& i) : member(i.member) { }
Int& operator=(const Int& i);
// access
int data() const { return member; }
// modyfication
void set_member(int n) { member = n; }
// operators
const Int& operator+() {return *this; } // unary+ operator
const Int operator-() {return Int(-this->member); } //unary- operator
const Int& operator++() {++member; return *this; } // prefix ++ increment operator
const Int operator++(int) {Int Old_value(*this); ++member; return Old_value; } // postfix ++ increment operator
const Int& operator--() {--member; return *this; } // prefix ++ increment operator
const Int operator--(int) {Int Old_value(*this); --member; return Old_value; } // postfix ++ increment operator
private:
int member;
};
//-------------------
// members
// assignment operator
Int& Int::operator=(const Int& i) {
if(this==&i) return *this;
member = i.member;
return *this;
}
//-------------------
// not members
// binary operator+
const Int operator+(const Int& left, const Int& right) {
return Int(left.data()+right.data());
}
//-------------------
// binary operator-
const Int operator-(const Int& left, const Int& right) {
return Int(left.data()-right.data());
}
//-------------------
// binary operator*
const Int operator*(const Int& left, const Int& right) {
return Int(left.data()*right.data());
}
//-------------------
// binary operator/
const Int operator/(const Int& left, const Int& right) {
return Int(left.data()/right.data());
}
//-------------------
// binary operator%
const Int operator%(const Int& left, const Int& right) {
return Int(left.data()%right.data());
}
//-------------------
// operator>>
istream& operator>>(istream& is, Int& i) {
int n;
is >> n;
if (is) i.set_member(n);
return is;
}
//-------------------
// operator<<
ostream& operator<<(ostream& os, const Int& i) {
os << i.data();
return os;
} |
|
и второй (шаблонный класс на основе первого класса Int)
C++ | 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
| //-------------------------------------------------------
// class template<class T> Number
template<class T>
class Number {
public:
// constructors
Number(T n = 0) : member(n) { }
Number(const Number& i) : member(i.member) { }
Number& operator=(const Number& n);
// access
T data() const { return member; }
// modyfication
void set_member(T n) { member = n; }
// operators
const Number& operator+() {return *this; } // unary+ operator
const Number operator-() {return Number(-member); } //unary- operator
const Number& operator++() {member++; return *this; } // prefix ++ increment operator
const Number operator++(int) {Number Old_value(*this); member++; return Old_value; } // postfix ++ increment operator
const Number& operator--() {member--; return *this; } // prefix ++ increment operator
const Number operator--(int) {Number Old_value(*this); member--; return Old_value; } // postfix ++ increment operator
private:
T member;
};
//-------------------
// members
// assignment operator
template<class T>
Number<T>& Number<T>::operator=(const Number<T>& n) {
if(this==&n) return *this;
member = n.member;
return *this;
}
//-------------------
// not members
// binary operator+
template<class T>
const Number<T> operator+(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()+right.data());
}
//-------------------
// binary operator-
template<class T>
const Number<T> operator-(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()-right.data());
}
//-------------------
// binary operator*
template<class T>
const Number<T> operator*(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()*right.data());
}
//-------------------
// binary operator/
template<class T>
const Number<T> operator/(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()/right.data());
}
//-------------------
// binary operator%
template<class T>
const Number<T> operator%(const Number<T>& left, const Number<T>& right) {
return Number<T>(left.data()%right.data());
}
//-------------------
// operator>>
template<class T>
istream& operator>>(istream& is, Number<T>& i) {
T n;
is >> n;
if (is) i.set_member(n);
return is;
}
//-------------------
// operator<<
template<class T>
ostream& operator<<(ostream& os, const Number<T>& i) {
os << i.data();
return os;
} |
|
Содержимое main()
C++ | 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
| int main()
{
Int i(10);
cout << -i << "; and value is " << i << endl;
cout << --i << "; and value is " << i << endl;
cout << +i << "; and value is " << i << endl;
cout << ++i << "; and value is " << i << endl;
cout << "postfix operators" << endl;
cout << i++ << "; and value is " << i << endl;
cout << i-- << "; and value is " << i << endl;
cout << endl << "-----------------" << endl;
Number<int> n1(100);
Number<int> n2(5);
cout << n1 << " % " << n2 << " == " <<n1%n2 << endl;
cout << n1 << " / " << n2 << " == " <<n1/n2 << endl;
cout << n1 << " * " << n2 << " == " <<n1*n2 << endl;
cout << n1 << " - " << n2 << " == " <<n1-n2 << endl;
cout << n1 << " + " << n2 << " == " <<n1+n2 << endl;
cout << "n1 == 100; correct work:" << endl;
cout << n1;
cout << " prefix-- == " << --n1 << endl;
cout << n1;
cout << " postfix++ == " << n1++ << "; and n1 value == " << n1 << endl;
cout << "----------------" << endl;
cout << "n1 == 100; incorrect work:" << endl;
cout << n1 << " prefix-- == " << --n1 << endl;
cout << n1 << " postfix++ == " << n1++ << "; and n1 value == " << n1 << endl;
return 0;
} |
|
При выводе постфиксного и префиксного инкремента/декремента шаблонного класса Number используя поток cout и несколько операторов<<
C++ | 1
2
3
| cout << "n == 100; incorrect work:" << endl;
cout << n1 << " prefix-- == " << --n1 << endl;
cout << n1 << " postfix++ == " << n1++ << "; and n1 value == " << n1 << endl; |
|
получаем несовсем то, что ожидалось.
Почему так?
Добавлено через 6 минут
Вывод:
correct work:
100 prefix-- == 99
99 postfix++ == 99; and n1 value == 100
----------------
incorrect work:
99 prefix-- == 99
100 postfix++ == 99; and n1 value == 100
0
|