Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MorphingDragon

macrumors 603
Original poster
Mar 27, 2009
5,159
6
The World Inbetween
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.
 
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.


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.
 
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
 
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

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.
 
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

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.
 
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.

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
 
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.

:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.