Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 2240 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Devvie

« 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
     Vector2D a = new Vector2D(100, 200);
     Vector2D b = new Vector2D(100, 200);

     Vector2D x = a + b;

     ( x == Vector2D(200, 400) )


    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
Global Moderator

JGO Wizard

Medals: 75
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++ .


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 Devvie

« 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 Wizard

Medals: 143
Exp: 18 years

Computers can do that?

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

If you get spaghetti code with 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

JGO Kernel

Medals: 517

« Reply #5 - Posted 2010-07-16 10:43:55 »

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

Junior Devvie

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

Use scala.
Pages: [1]
  ignore  |  Print  

EgonOlsen (74 views)
2018-06-10 19:43:48

EgonOlsen (55 views)
2018-06-10 19:43:44

EgonOlsen (74 views)
2018-06-10 19:43:20

DesertCoockie (254 views)
2018-05-13 18:23:11

nelsongames (154 views)
2018-04-24 18:15:36

nelsongames (154 views)
2018-04-24 18:14:32

ivj94 (895 views)
2018-03-24 14:47:39

ivj94 (156 views)
2018-03-24 14:46:31

ivj94 (808 views)
2018-03-24 14:43:53

Solater (172 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!