# Thread: How do you use operator+()? I'm confused!

1. ## How do you use operator+()? I'm confused!

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?

2. In follow-up:
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 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?!?!

Thanks

3. focus more on Tazar's queston please.

4. 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...

5. 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.

6. 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 first one is the binary operator+ which gets called when you place it between two operands:
Code:
`v1 + v2; // same as: v1.operator+(v2);`
The second one is the unary operator+ which gets called if you place it before a single operand:
Code:
`+v1; // same as: v1.operator+();`