Method overloading in Objective C

Discussion in 'Mac Programming' started by akabeer1983, Jan 12, 2009.

  1. akabeer1983 macrumors newbie

    Jan 7, 2009
    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

    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.) "
  2. toddburch macrumors 6502a

    Dec 4, 2006
    Katy, Texas
  3. kpua macrumors 6502

    Jul 25, 2006

    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:

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

Share This Page