Jump to content
  • Advertisement
Sign in to follow this  
SgtSparky

Java - class declared operators

This topic is 3259 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

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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!