I know that for + and - you use the following code;
But can I use similar codes for *, /, and ^? Are they the same?Code:complexClass complexClass::operator+ (complexClass a){}
This is a discussion on Operator overload question within the C++ Programming forums, part of the General Programming Boards category; I know that for + and - you use the following code; Code: complexClass complexClass::operator+ (complexClass a){} But can I ...
I know that for + and - you use the following code;
But can I use similar codes for *, /, and ^? Are they the same?Code:complexClass complexClass::operator+ (complexClass a){}
Last edited by 843; 03-13-2011 at 11:52 AM.
Yes.
Pass by const reference:
Code:complexClass complexClass::operator+ (const complexClass& a){}
I never put signature, but I decided to make an exception.
Why do I need const and to pass by reference? Why don't the + and - operators need to?
no that's not right, if you use the one-parameter member function version then it should also be a const function.
However, for the operators that return a result by value, e.g. +, -, *, /, |, ^, & etc, you should use the two-parameter friend, static or non-member version.
e.g.Code:friend complexClass complexClass::operator+ (const complexClass &a, const complexClass &b) { complexClass c; // ... blah blah ... return c; }
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
I see now. Thanks for all the help!