Sign in to follow this  

Operator overloading

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Something I have wondered for a long time... 2 overloaded functions:
Vector3 Vector3 :: operator - ()
{
	return Vector3 (-x, -y, -z);
};

void Vector3 :: operator - (int)
{
	x = -x;
	y = -y;
	z = -z;
};
The int is only there to provide a unique parameter list for compilation. Does it slow the final code down at all, and is there a better way?

Share this post


Link to post
Share on other sites
In this case, the compiler optimizes it away. It'll always do that for operator-- and ++. It's only there so the compiler has a unique symbol, as you said. There's no better way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
In this case, the compiler optimizes it away. It'll always do that for operator-- and ++. It's only there so the compiler has a unique symbol, as you said. There's no better way.


That's not entirely true. It only gets optimized away if the function is inlined, assuming your compiler's optimizer isn't idiotic. If the function is not inlined, the program will pass in a 0 as the integer argument. Of course, it's still highly unlikely to be a source of performance problems on modern computers.

Share this post


Link to post
Share on other sites
Quote:
Original post by guin

Vector3 Vector3 :: operator - ()
void Vector3 :: operator - (int)


The first operator is the negation operator. The second operator is the subtraction operator (though your implementation has problems).

Perhaps you are thinking of the increment (++) and decrement (--) operators, which have an unused parameter whose sole purpose is to discriminate between pre- and post-.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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