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

akabeer1983

macrumors newbie
Original poster
Jan 7, 2009
5
0
hi, Does objective C support method overloading.
suppose there are 2 methods:
(int)add(int)X : (int) Y;
(float)add(float)X : (float) Y;

The selector for the two methods is the same
add::

But the runtime would be confused as to which method to invoke based on this selector, so i think method overloading is not supported.
I also came through the following paragraph in Objective C 2.0 PDF, i could not understand this. Can anyone help me?

"The messaging routine has access to method implementations only through selectors, so it treats all
methods with the same selector alike. It discovers the return type of a method, and the data types of
its arguments, from the selector. Therefore, except for messages sent to statically typed receivers,
dynamic binding requires all implementations of identically named methods to have the same return
type and the same argument types. (Statically typed receivers are an exception to this rule, since the
compiler can learn about the method implementation from the class type.) "
 

kpua

macrumors 6502
Jul 25, 2006
294
0
No.

To get around this limitation, Objective-C has a convention for selector names where you put the name of the parameter type before the colon, like this:

Code:
- (int)addInt:(int)x toInt:(int)y;
- (float)addFloat:(float)x toFloat:(float)y;

This is pretty typical style, although the word before the argument isn't always a C type. It's usually the name of a generic class type like "image" or "view" or something that describes what the thing is like "filename" (even though the parameter types is an NSString).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.