Calling a method when arithmetic operators are used on objects...

Discussion in 'Mac Programming' started by MorphingDragon, Sep 13, 2010.

  1. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Messages:
    5,161
    Location:
    The World Inbetween
    #1
    Calling a method when arithmetic operators are used on objects, Is this possible in Objective-C bar some sort of C macro.

    EG:

    Code:
    myFraction = aFraction * anotherFraction; 
    
    //Is the same as
    
    myFraction = [aFraction multiplyBy: anotherFraction];
    It seems such a waste of time and readability to call such methods on such objects when they only represent numbers.
     
  2. macrumors regular

    Joined:
    Aug 26, 2010
    Messages:
    223
    #2

    If you've created the fraction class than it's as simple as adding a new method. If not than you will need to either subclass the fraction class or use categories.

    I would recommend reading on both subclassing and categories.
     
  3. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Messages:
    5,161
    Location:
    The World Inbetween
    #3
    No I mean so you can just use * / + - on objects instead of multiplyBy: divideBy: methods etc.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Messages:
    3,172
    Location:
    Dallas, TX
    #4
    This is called operator overloading, and Objective-C does not support this feature. I appreciate this because you know exactly what's going on at a glance. There are good arguments for overloading, but for now Objective-C doesn't have it, so message passing is where it's at. With code completion it's not much more typing, and I think it's still quite readable.

    -Lee
     
  5. macrumors 6502a

    Joined:
    Jan 15, 2007
    Messages:
    902
    #5
    Though I wish they would make an exception for strings(which would be sacrelig, I know!), doing even basic string manipulation can get ugly quick and looks bad to boot.
     
  6. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Messages:
    5,161
    Location:
    The World Inbetween
    #6
    I would only really use it so the code is more like natural arithmetic.

    I had a look at some of the C# and C++ implementation and gotchas... they look painful.
     
  7. macrumors regular

    Joined:
    Aug 26, 2010
    Messages:
    223
    #7

    Oops my fault, I didn't read the entire post. I'm going to blame it on reading the post at 8:00am on my IPad.
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Messages:
    3,172
    Location:
    Dallas, TX
    #8
    I know you're going through a book right now, but in a "real world" scenario where you had wrappers that you wanted to do arithmetic with it would be best to have some manner to link operations so you don't have a lot of objects being created as intermediate values of the calculation just to be thrown away. Just something to keep in mind for performance' sake.

    -Lee
     
  9. macrumors 6502

    Joined:
    Apr 24, 2008
    Messages:
    471
    #9
    I simply can't bring myself to stay out of a thread about operator overloading. I'll just sit back and smile though, and not ruin anybody's mood.

    :)
     

Share This Page