Pos/Pre fix operator overloading question.

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

Join Date: Oct 2008
Posts: 25
Reputation: konyango is an unknown quantity at this point 
Solved Threads: 0
konyango's Avatar
konyango konyango is offline Offline
Light Poster

Pos/Pre fix operator overloading question.

 
0
  #1
Jun 26th, 2009
Hello everyone. I have been reading c++ in the book "C++ : A beginners Guide" and I have some questions about overloading a unary operator.

This is the code I have been working on/trying to understand :

/* Demonstrating overloading a unary operator
with a friedn function(prefix and postfix) */
#include<iostream>
using namespace std ;

class ThreeD {
int x, y, z ;
public:
ThreeD( ) { x = y = z = 0 ; }
ThreeD(int i,int j,int k) { x = i ; y = j ; z = k ; }
friend ThreeD operator++(ThreeD &op1) ;
friend ThreeD operator++(ThreeD &op1,int notused) ;
void show( ) ;
} ;

ThreeD operator++(ThreeD &op1)
{
op1.x++ ;
op1.y++ ;
op1.z++ ;
return op1 ;
}

ThreeD operator++(ThreeD &op1, int notused)
{
ThreeD temp = op1 ;
op1.x++ ;
op1.y++ ;
op1.z++ ;
return temp ;
}

void ThreeD::show( )
{
cout << x << ", " << y << ", " << z << "\n\n" ;
}

int main( )
{
ThreeD a(1,2,3) ;
ThreeD b ;

cout << "Original a value : " ;
a.show( ) ;
++a ;
cout << "a after ++a : " ;
a.show( ) ;

cout << "Original b value : " ;
b.show( ) ;
b = a ;
cout << "b after b = a : " ;
b.show( ) ;
b++ ;
cout << "b after b++ : " ;
b.show( ) ;

return 0 ;
}

This is the question i have about this program and pre/post fix operator overloading concept :

Question : Why does the prefix/postfix version of the
operator overloaded function have to return a value ?
I don't understand why a value should be returned yet it is
a reference.

Your help will be appreciated.
Thank You.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Pos/Pre fix operator overloading question.

 
0
  #2
Jun 26th, 2009
So you can do things like a = b++ and a = ++b . Chaining and combining expressions accounts for most of the weirdness in how operator overloading works. But you don't have to follow the conventions even though it's a good idea to stick to idioms that everyone expects. Your operators could return void and still work for the basic cases of b++; and ++b; .
Last edited by Tom Gunn; Jun 26th, 2009 at 3:20 pm.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,364
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pos/Pre fix operator overloading question.

 
0
  #3
Jun 26th, 2009
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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