Java - class declared operators

Started by
2 comments, last by SgtSparky 14 years, 9 months ago
Hello. I would like to make operators for a 2D vector class. I know how to do this easily in C++, but how would I add these operators in Java? Thanks, --SgtSparky
--Evan
Advertisement
Unfortunately, you cannot overload operators in Java.

You've got to do it all through function calls, e.g.
Vec2d a = new Vec2d;Vec2d b = new Vec2d( 0, 1 );Vec2d c = new Vec2d( 3, 0 );a.AssignSum( b, c );//a now equals (3,1)
Damn you Java...
Java doesn't support operator overloading. You just have to write methods that will do the operations..

for instance:

Vector vec1 = new Vector (1.0f, 2.0f, 3.0f);
Vector vec2 = new Vector (5.0f, 5.0f, 5.0f);

Vector vec3 = vec1.dotProduct (vec2);

or similar..
Quote:Original post by Hodgman
Unfortunately, you cannot overload operators in Java.

You've got to do it all through function calls, e.g.*** Source Snippet Removed ***Damn you Java...


Dang, I was afraid of that. D:

Thanks anyways!
--Evan

This topic is closed to new replies.

Advertisement