We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
overload arithmetic operators (Read 1576 times)
overload arithmetic operators
May 20th, 2005, 8:37pm
 
I'm not sure quite how to word this, so I'll give an example.

I'm writing my own vector class (again), but this time I want to give them the ability to do something like this in code:

vec3 = vec1 + vec2

instead of writing

vec3=vec1.add(vec2)

Each "vec" is of a vector class that I define. Is it possible in Java to write arithmetic operators for your own class type? I don't even know how to begin searching on google for this.

Thanks!


edit:
Okay I did some more digging. Apparently it's not supported natively by Java, however some humans have written a preprocessor that take care of this called Jfront:
http://www.winternet.com/~gginc/jfront/

I wonder if processing could have something like this in the future?
Re: overload arithmetic operators
Reply #1 - May 24th, 2005, 11:38pm
 
Comming from POVray, I feel the same problem. With an intense use of vectors classes, I tried many ways to rewrite my vector3D class. I didn't found any way to bypass this (as in your example).

With this form vec3=vec1.add(vec2), it become rapidly unreadable or I feel that I've to think backward. I get lost. I guess that with overloads, it will be much more easier to work with vectors.

I still have this problem. I didn't found any way to do it or to do something similar in Java. Working with overloads is not permitted in the purist programming form. To bypass this, I was thinking to write my own geometric pseudo language in a separate .txt file. An unelegant way to do things I guess.
Re: overload arithmetic operators
Reply #2 - May 25th, 2005, 12:58am
 
I was just wondering yesterday if it's possible to overload operators in processing... would be very nice Smiley
Re: overload arithmetic operators
Reply #3 - May 25th, 2005, 3:14am
 
yeah, would be nice, though probably not something we'll get to before 1.0, since it involves a good bit of preprocessor/compiler code to get it to behave properly.
Page Index Toggle Pages: 1