• ### What is your GameDev Story?

This topic is 4494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, Can anyone tell me how to overload the unary operator -(or +)? Specifically, I've created a vector class that overloads the - operator so that I can do: vector3 = vector2 - vector1; Now I want to be able to do: vector3 = -vector1; How can I achieve this? Any example? Thanks

##### Share on other sites
One way to do it is as follows:
Vector3 Vector3::operator-() const{    return Vector3(-x,-y,-z);}

##### Share on other sites
So, it was so easy after all.

Thanks a lot.

How does
V3 = V2 - V1

also equal

V3 = -V1

##### Share on other sites
Quote:
 Original post by Anonymous PosterHow does V3 = V2 - V1also equalV3 = -V1
He was just saying that he'd already overloaded the binary - operator to perform subtraction, but was unsure of how to overload unary - to perform negation (at least that's how I interpreted it).

##### Share on other sites
It doesn't, and he never said it did. He was just using it as an example.

##### Share on other sites
Quote:
 Original post by Anonymous PosterHow does V3 = V2 - V1also equalV3 = -V1

First of all, = doesnt mean EQUAL.
And I meant what exactly jyk understood.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015573
×