| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | C++ - overloading pre/post-increment/decrement operators |
> Can anyone tell me why it should be (or is) necessary to
> provide the capability to overload both pre and post
> operators? Surely one is interchangeable with the other,
No, they aren't interchangable. The effect of:
int x,i;
x = i++;
is clearly different to
int x,i;
x = ++i;
Why shouldn't more complex objects behave in much the same fashion?
> the compiler should determine to call the overload function
> either before or after the expression it's in is evaluated.
> Is there something I'm missing here?
There's a logical difference between the two.
Say we put the increment operators (both post- and pre-) into an iterator
class. It might have the following members:
class Iterator
{
public:
// ...
int operator++();
int operator++(int);
int current(); // There is valid "current"
// ...
};
Now the result of the ++ operators might differ according to function -
logically it should. On the one hand, the pre-increment operator would
return the state AFTER the iterator moves to the 'next', and BEFORE in the
post-iterator case.
This rationale, like all operator overloading in C++, is on the basis that
classes should be allowed to behave as much like intrinsic types as
possible, so that expressions LOOK logical and have a more intuitive
meaning.
> if( C++ == ++C ) exit(ERROR);
Ah, the behaviour in this expression is "undefined" under ISO C
(and therefore C++). You might get different results with different
compilers, but any which way it turns it, it is definitely not an error.
:-)
---
* Origin: Unique Computing, Melbourne, Australia (3:632/348)SEEN-BY: 50/99 54/54 620/243 623/630 632/103 301 348 386 998 633/371 379 SEEN-BY: 634/384 635/301 502 503 541 544 727 636/100 639/100 711/401 409 410 SEEN-BY: 711/430 510 807 808 809 932 934 712/515 713/888 714/906 800/1 SEEN-BY: 7877/2809 @PATH: 632/103 348 635/503 50/99 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.