I'm not sure how to use operator+(). Can you use it on just regular old integers or does it have to be done with classes?
I'm not sure how to use operator+(). Can you use it on just regular old integers or does it have to be done with classes?
In follow-up:
I don't get why the operator+() is there for..?Code:// vector add const CVector operator+(const CVector &vec) const { return CVector(x + vec.x, y + vec.y, z + vec.z); } // vector add (opposite of negation) const CVector operator+() const { return CVector(*this); }
I understand the first operator+(bla), but why is the second there, what does it do, when does it get called, and how does it work?!?!
Thanks
focus more on Tazar's queston please.
Thats just using function overloading (and obviously; Operator overloading). In essence, if the function is called without parameters, the second function is executed. If called with [parameters], the first function is executed.
The second function returns the object while the first returns the two added CVectors.
>>Can you use it on just regular old integers or does it have to be done with classes?
It can do anything you want. You're overloading it for your own use after all. The point is to keep it intuitive though...
"There's always another way"
-lightatdawn (lightatdawn.cprogramming.com)
the answer to your question is in the comment. Its the opposite of negation.
say your object was an int 5 and you applied the negation operator
int int::operator -()
then the return would be -5.
operator +() does just the opposite.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
The first one is the binary operator+ which gets called when you place it between two operands:Tazar_Azar wrote:
I don't get why the operator+() is there for..?
I understand the first operator+(bla), but why is the second there, what does it do, when does it get called, and how does it work?!?!The second one is the unary operator+ which gets called if you place it before a single operand:Code:v1 + v2; // same as: v1.operator+(v2);Code:+v1; // same as: v1.operator+();
- lmov