| | |
a small prob with operator overloading
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
suppose u r makin a class to carry out oprations on complex numbers...
over loading the '+' operator for adding objects' made frm dat class is simple.....
but how would u carry out this operation???
a= 10 + b; // it's not (b+10)
where a & b are objects of the complex no. class.........
Hey you are not a newbie; how could you post it in the C forum?!
Considering that you are talking about C++, the problem you are facing can be eliminated by using friend functions.
Considering that you are talking about C++, the problem you are facing can be eliminated by using friend functions.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> but how would u carry out this operation???
a= 10 + b; // it's not (b+10)
>> the problem you are facing can be eliminated by using friend functions.
friend functions are functions which are not members of the class, but are granted the same access rights as members of the class. they may be either free functions or member functions of another class.
operators (not all) can be overloaded using free functions.
the ideas are orthogonal; a free functions which oveloads an operator need not be a friend function.
a= 10 + b; // it's not (b+10)
>> the problem you are facing can be eliminated by using friend functions.
friend functions are functions which are not members of the class, but are granted the same access rights as members of the class. they may be either free functions or member functions of another class.
operators (not all) can be overloaded using free functions.
the ideas are orthogonal; a free functions which oveloads an operator need not be a friend function.
c++ Syntax (Toggle Plain Text)
#include <iostream> struct modulo7 { explicit modulo7( int i ) : number(i%7) {} inline int value() const { return number ; } private: int number ; }; inline modulo7 operator+( modulo7 first, modulo7 second ) { return modulo7( (first.value()+second.value()) % 7 ) ; } inline modulo7 operator+( modulo7 first, int second ) { return modulo7( (first.value()+second) % 7 ) ; } inline modulo7 operator+( int first, modulo7 second ) { return second + first ; } template< typename stream_type > inline stream_type& operator<< ( stream_type& stm, modulo7 m ) { return stm << m.value() ; } int main() { modulo7 a(6), b(4) ; std::cout << a+4 << ' ' << 4+a << ' ' << a+b << '\n' ; }
![]() |
Similar Threads
- Operator Overloading HELP! (C++)
- about operator overloading (C++)
- operator overloading (Java)
- 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: take two value from one array
- Next Thread: factorial
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list 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 rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






