943,719 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2175
  • C++ RSS
Feb 17th, 2008
0

operator overload

Expand Post »
Hi
when I look at this website http://www.parashift.com/c++-faq-lit...erloading.html,
I don't quite understand for the section 13.14 about prefix++ and postfix++.

Number x = /* ... */;
++x; // calls Number::operator++(), i.e., calls x.operator++()
x++; // calls Number::operator++(int), i.e., calls x.operator++(0)
why ++x is calling x.operator++() while x++ is calling calls x.operator++(0)?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dophine is offline Offline
26 posts
since Feb 2008
Feb 18th, 2008
1

Re: operator overload

This is the important part:
Quote ...
Since the prefix and postfix ++ operators can have two definitions, the C++ language gives us two different signatures. Both are called operator++(), but the prefix version takes no parameters and the postfix version takes a dummy int.
In other words, if one didn't have a dummy int, there'd be no way to disambiguate them.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 18th, 2008
0

Re: operator overload

but I don't understand how to know there is a dummy int for x++ while ++x does not.
Reputation Points: 10
Solved Threads: 0
Light Poster
dophine is offline Offline
26 posts
since Feb 2008
Feb 18th, 2008
0

Re: operator overload

If you would have been given an options to implement operator++() which would work for both versions of ++, how would you do it?
You need to find some way to distinguish between both of them.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Feb 18th, 2008
0

Re: operator overload

Quote ...
Number x = /* ... */;
++x; // calls Number::operator++(), i.e., calls x.operator++()
x++; // calls Number::operator++(int), i.e., calls x.operator++(0)
Seems to me that you answered your own question with the above.
That IS the overload method.
As they say: "It is what it is."
You looked it up, NOW YOU KNOW...
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Feb 18th, 2008
0

Re: operator overload

thank you all.
say for example, objectA=objectB, then I know objectA.operator=(objectB)
but for ++x and x++, I don't know x.operator++() or x.operator++(0) will be called
Reputation Points: 10
Solved Threads: 0
Light Poster
dophine is offline Offline
26 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: simple question regarding array
Next Thread in C++ Forum Timeline: Sorting a Structure.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC