Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Operator overloading

Started by guin Apr 29, 2006 at 9:07 AM 2 replies 650+ views
Original Post
guin
guin
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?
Evil Steve
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.
Troll
Troll
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.
JohnBolton
JohnBolton
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-.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.