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){}
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){}
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"
I see now. Thanks for all the help!