Sign in to follow this  
Lord_Vader

operator overloading

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 this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How does
V3 = V2 - V1

also equal

V3 = -V1

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
How does
V3 = V2 - V1

also equal

V3 = -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 this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
How does
V3 = V2 - V1

also equal

V3 = -V1


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this