| | |
Pos/Pre fix operator overloading question.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
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
![]() |
Similar Threads
- Operator Overloading (C++)
- need help in operator overloading...? (C++)
- Overloading the increment operator plz help (C++)
- Operator Overloading Question (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- program for finding factorial of a number using *operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ rapi application
- Next Thread: writting .doc file in Win32 using FILE pointer.
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






