Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Writing classes that use operators ( + - / * etc)  (Read 1282 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Duke





« Posted 2010-07-16 01:33:11 »

A rather general question Smiley

Is it possible to create classes that use the comparator and combination operators?

For example, I would like to be able to write something like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
     Vector2D a = new Vector2D(100, 200);
     Vector2D b = new Vector2D(100, 200);

     Vector2D x = a + b;

     ( x == Vector2D(200, 400) )

    OR

    boolean x = (a > b);

    ( x == false )


Writing it that way is far more intuitive (for me anyway), than say having a method Vector2D.Add(Vector2D a, Vector2D b);

I seem to remember that this is possible in either C++ or C#, but cant recall where I saw the example.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2010-07-16 01:57:42 »

It is not . This is called operator overloading . It does exist for the String class ("a"+"b" = "ab") but that's about it.
You cannot define your own operators overloading like C++ .

yep.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2010-07-16 02:20:35 »

Yeah, you'll basically need to put functions with relevant names myObject.add(myObject2) myObject.scale(myObject2) etc.

I personally like this because if you've seen a game engine in another language (take the Unreal Engine) you'll go insane over how many ridiculous operators they have, like using ^ to represent the dot product (not remembering exactly, but you get the idea).

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dreamcatchermatt

Junior Duke





« Reply #3 - Posted 2010-07-16 03:19:34 »

well bummer  Undecided

like C++ .
ohh yeh.

you'll basically need to put functions with relevant names myObject.add(myObject2) myObject.scale(myObject2) etc.
Yeh, that's what I've been doing. Unfortunately at times it leads to massive spaghetti-code method calls which i write at 3 in the morning and then have to spend an hour unpicking in the cold light of day Smiley

Thanks anyway guys
Offline delt0r

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #4 - Posted 2010-07-16 10:40:32 »

If you get spaghetti code with a.plus(b).minus(c).scale(30)..... Its still spaghetti code with operator overloading. With the added obfuscation on what the operator is really doing (I won't get into some doggy things folks do with types and templates). C++ also forces "copy" semantics IIRC where you can define your own with explicit methods and document them. You can even provide different semantics in the same object, like addTo() and add() for example. 

One piece of code i was working on had a simple c=a+b+c; statement. Only thing was it didn't really add a b or c and the result assigned to c was so full of side effects we rewrote everything with explicit add methods.

I would not use operator overloading very often if at all, even when its available (One exception would be a complex number class for example, but again forced copy semantics may remove the benefit).

I have no special talents. I am only passionately curious.--Albert Einstein
Offline Roquen
« Reply #5 - Posted 2010-07-16 10:43:55 »

Oh no!  Let's not go there again!
Offline i30817

Junior Duke





« Reply #6 - Posted 2010-08-05 20:19:30 »

Use scala.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (13 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!