k*******3 发帖数: 1909 | 1 essential C++ 中文版4.6节“打造一个iterator class”中分别定义了++的前置和后
置版
本,请问为啥一个返回Triangular_iterator&,而一个返回Triangular_iterator类呢?
谢谢。
inline Triangular_iterator& Triangular_iterator::
operator++()
{ // prefix instance
++_index;
check_integrity();
return *this;
}
inline Triangular_iterator Triangular_iterator::
operator++( int )
{ // postfix instance
Triangular_iterator tmp = *this;
++_index;
check_integrity();
return tmp;
} |
x****u 发帖数: 44466 | 2 引用就是指针
呢?
【在 k*******3 的大作中提到】 : essential C++ 中文版4.6节“打造一个iterator class”中分别定义了++的前置和后 : 置版 : 本,请问为啥一个返回Triangular_iterator&,而一个返回Triangular_iterator类呢? : 谢谢。 : inline Triangular_iterator& Triangular_iterator:: : operator++() : { // prefix instance : ++_index; : check_integrity(); : return *this;
|
k*******3 发帖数: 1909 | 3 为啥前置++和后置++一个返回值一个返回引用呢?
【在 x****u 的大作中提到】 : 引用就是指针 : : 呢?
|
f*******n 发帖数: 12623 | 4 operator++() is for pre-increment (++x)
operator++(int) is for post-increment (x++)
Because both pre and post are unary operators, there is no way to tell them
apart, so C++ made a special case, and post-increment / post-decrement take
an additional int parameter.
Pre-increment returns a reference, because a pre-increment expression is an
lvalue (you can assign to it or take the address of it, etc.). Post-
increment returns a copy, because a post-increment expression is a rvalue.
呢?
【在 k*******3 的大作中提到】 : essential C++ 中文版4.6节“打造一个iterator class”中分别定义了++的前置和后 : 置版 : 本,请问为啥一个返回Triangular_iterator&,而一个返回Triangular_iterator类呢? : 谢谢。 : inline Triangular_iterator& Triangular_iterator:: : operator++() : { // prefix instance : ++_index; : check_integrity(); : return *this;
|
k*******3 发帖数: 1909 | 5 高! 学习了。谢谢
them
take
an
【在 f*******n 的大作中提到】 : operator++() is for pre-increment (++x) : operator++(int) is for post-increment (x++) : Because both pre and post are unary operators, there is no way to tell them : apart, so C++ made a special case, and post-increment / post-decrement take : an additional int parameter. : Pre-increment returns a reference, because a pre-increment expression is an : lvalue (you can assign to it or take the address of it, etc.). Post- : increment returns a copy, because a post-increment expression is a rvalue. : : 呢?
|