z****e 发帖数: 2024 | 1 class Number {
public:
Number& operator++ (); // prefix ++
Number operator++ (int); // postfix ++
};
why should the prefix version returns by reference, the postfix version by
value? why not both by reference? |
X****r 发帖数: 3557 | 2 Just try to write an overloaded postfix++ that returns a reference,
you'll understand.
by
【在 z****e 的大作中提到】 : class Number { : public: : Number& operator++ (); // prefix ++ : Number operator++ (int); // postfix ++ : }; : why should the prefix version returns by reference, the postfix version by : value? why not both by reference?
|
S*********g 发帖数: 5298 | 3 I would suggest OP to read thinking in C++ about this issue.
There is a full discussion about this.
【在 X****r 的大作中提到】 : Just try to write an overloaded postfix++ that returns a reference, : you'll understand. : : by
|
z****e 发帖数: 2024 | 4 i didn't write it, but i suddenly realized, after reading your post, that
the tmp object will be destroyed after the postfix op++ returns. so must
return by value right?
【在 X****r 的大作中提到】 : Just try to write an overloaded postfix++ that returns a reference, : you'll understand. : : by
|
z****e 发帖数: 2024 | 5 actually, i worked it through one year ago page by page. but now, i forget
60% of it. so bad:(
how to remember, is still my trouble.
【在 S*********g 的大作中提到】 : I would suggest OP to read thinking in C++ about this issue. : There is a full discussion about this.
|
S*********g 发帖数: 5298 | 6 well, you have to understand the concept not just memorizing the result
【在 z****e 的大作中提到】 : actually, i worked it through one year ago page by page. but now, i forget : 60% of it. so bad:( : how to remember, is still my trouble.
|
X****r 发帖数: 3557 | 7 Totally agree, and I should go further to say there should be very
little thing to memorize from any book at all. Everything worth
learning is either fact or logic. For fact you can always look it up
later when needed so you only need to know where is the reference.
For logic you should be able to deduce it yourself if you really
understand it. Thus my advice for beginner is that don't try too
hard to memorize anything.
【在 S*********g 的大作中提到】 : well, you have to understand the concept not just memorizing the result
|
z****e 发帖数: 2024 | 8 谨遵师嘱。
我就在理解的基础上记忆吧。
【在 X****r 的大作中提到】 : Totally agree, and I should go further to say there should be very : little thing to memorize from any book at all. Everything worth : learning is either fact or logic. For fact you can always look it up : later when needed so you only need to know where is the reference. : For logic you should be able to deduce it yourself if you really : understand it. Thus my advice for beginner is that don't try too : hard to memorize anything.
|