operator overload

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 15
Reputation: dophine is an unknown quantity at this point 
Solved Threads: 0
dophine dophine is offline Offline
Newbie Poster

operator overload

 
0
  #1
Feb 17th, 2008
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)?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: operator overload

 
1
  #2
Feb 18th, 2008
This is the important part:
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 15
Reputation: dophine is an unknown quantity at this point 
Solved Threads: 0
dophine dophine is offline Offline
Newbie Poster

Re: operator overload

 
0
  #3
Feb 18th, 2008
but I don't understand how to know there is a dummy int for x++ while ++x does not.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: operator overload

 
0
  #4
Feb 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: operator overload

 
0
  #5
Feb 18th, 2008
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...
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 15
Reputation: dophine is an unknown quantity at this point 
Solved Threads: 0
dophine dophine is offline Offline
Newbie Poster

Re: operator overload

 
0
  #6
Feb 18th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC