Jump to content
  • Advertisement
Sign in to follow this  
Pirosan

[java] java method operator question

This topic is 4855 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 have a functino in C that i want to use in java... one of the methods in c sets the operator like so: glQuaternion glQuaternion::operator *(glQuaternion q){} basically it will allow me to create this object then multiply two objects together, and i dont know how to use the equivalent in java... how can i do this? cheers

Share this post


Link to post
Share on other sites
Advertisement
Yep, just can't do that in Java. I think it was decided that it was one of those "dangerous" and/or "confusing" etc. things so they left it out. Although, they did kind of overload "+" for String concatenation.

I guess the Java alternative would be to provide a method that takes the object that you're multiplying as a parameter, and does the math there. It could either save the new value in the object that the method is being called for, or return a brand new object with the new value. There's probably a better/another way, maybe someone will suggest one.

Share this post


Link to post
Share on other sites
yeah im giving my class a multiply method that takes in another class like its own, does the calculation, then spits out a new object. Thanks for the help

Share this post


Link to post
Share on other sites
You'll instead have to either create a function for which an instance takes an object of the same class, as in the following...

public class MyNumber {
...
public void multiply(MyNumber lhs) {
// Use "this" and "lhs" to calculate, and return the new
// value of of type MyNumber that represents the result.
}
...
}


...or else create a static function that takes two parameters...

public class MyNumber {
...
public static MyNumber multiply(MyNumber lhs, MyNumber rhs) {
// User "lhs" and "rhs" to calculate and return the new
// object of type MyNumber that represents the result.
}
}


Good luck!

Share this post


Link to post
Share on other sites
Operator overloading wasn't so much removed, as it was never added. AFAIK, they didn't add it because it made the language too unreadable. I, however, think the benefits outweigh the drawbacks.

Another one of the things they didn't add (at first) was templates. The Generics system they've created is a poor substitute, IMHO. Generics are only used by the compiler, not the JVM. The code looks the same after compilation whether you use Generics or not.

/rant

Share this post


Link to post
Share on other sites

// C++
Product a, b, c, d;
a += b;
d = a + c;


You can see this, and without access to the Product class implementation, try to guess what the hell these operations mean. Am I simply adding the prices of the products, or compounding a "kit" of products? In most (distributed) systems, you don't have access to all the implementation details while you're developing.

And about the generics, I agree, but I still find it a valid feature. If it were not for syntatic sugar, we would be still programming in assembly (so says the signature of some member of gamedev's forums that I can't recall the name =). I just hope that it will soon benefit from RTTI.

Son Of Cain

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!