I am learning objective-C after having good knowledge of C/C++. I have 2 questions:

  1. Are Obj-C message passing and C++ vtable, two different ways of implementing polymorphism or Are they two very different concepts which I have misinterpreted into different implementations of same concept?

  2. I read following line from this pdf http://www.gnustep.org/resources/documentation/ObjectivCBook.pdf

    Polymorphism takes a pluralistic point of view and notes that several classes can each have a method with the same name. Argument overloading takes the point of the view of the method name and notes that it can have different effects depending on what kind of object it applies to

    In essence both do what we want from them i.e. calling method according to the object. What is the difference then?

4 Answers 4

  1. Both are variants of dynamic dispatch (aka overloading resolved at execution time).

  2. Overloading is for me (I'm not an original, that's Cardelli and Weger classification) a kind of polymorphism. I'm not sure I understand what could be meant here. If I'm guessing correctly, your citation is making a difference between "polymorphism" which would be overloading on the special this parameter resolved at execution time using the dynamic type of the argument, and "overloading" which would be overloading on the other parameters resolved at compilation time (and thus using the static type of the arguments).

  • Overloading's sometimes known as "ad hoc polymorphism". May 25, 2012 at 14:21
  • @Frank, along with coercion (see figure 1 p. 4 in Cardelli's paper I've linked in my answer). May 25, 2012 at 14:35
  • @AProgrammer Your second point make sense to me! Thanks for the answer :)
    – codecool
    May 25, 2012 at 14:56

What AProgrammer said, with one crucial addition: message passing (a la Smalltalk, Objective-C, Ruby) permits the forwarding of a message (for transparent proxies) or even the runtime generation of a method implementing that message.

I'm using Smalltalk terminology, where a message is the name of the function/method being invoked. Objective-C folk would probably call this the selector. When an object receives a message, it looks up in some method dictionary a method corresponding to the message name - some function that actually does something. If no such method is found, the object's #doesNotUnderstand: (Smalltalk) or :method_missing? (Ruby) or - forwardInvocation: (Objective-C) is run.

Thus, an object receiving a message that it does not understand may react to that object in arbitrary ways. That includes, upon receiving such a message, creating a method that can handle this message, and installing that method in the object's method dictionary.

(This is definitely possible in Smalltalk where one normally has a compiler always available; it's probably possibly in Ruby, where you have instance_eval/class_eval. I don't know if it's possible in Objective-C; I don't know the language well enough.)

These things are not possible in languages that use vtable lookup.

  • Can you explain more about your these words - " even the runtime generation of a method implementing that message" ?
    – codecool
    May 25, 2012 at 14:58
  • I hope my edits answered your question. May 25, 2012 at 16:06
  • Yep, it cleared a lot
    – codecool
    May 25, 2012 at 16:40

They are very, very different mechanisms that provide vastly different features.

The C++ virtual dispatch mechanism is like a function pointer. The C++ compiler generates a construction function that fills a thunking table with pointers for calling functions. The lookup is as quick as calling void(*)(). This is completely static though and doesn't allow for replacing functions during runtime (although this could probably be done) expanding functionality or providing symbolic lookup like @signature("helloWorld"). If you want to do this in C++ you have to write it in, like Qt did and has a moc that generates the crap that has to occur.

The Objective-C message system is utterly different. There's this objc_msg_lookup or whatever function. Because in Objective-C the messages that an object can respond to can change on the fly, during runtime, there is absolutely no way to build a virtual dispatch like C++ has. Instead you have some sort of message id, maybe even a string since the system also has to be able to find @signatures or whatever they were, that is searched every time you make a call. This is why the language has the IMPL thingy that gives you a function pointer to call when you are in a tight loop or something.

It's been years since I've worked in Objective-C but I very much doubt this has changed.

Which is better? Neither. Each is better for different things.

  • +1 That's about right. The main dispatch function is actually called objc_msgSend(), and methods are referred to using @selectors.
    – Caleb
    May 25, 2012 at 19:54
  • They are different mechanisms put implement the same "basic" feature (polymorphism) (though because of the difference in implementation they have different twists). May 25, 2012 at 23:00
  • They are variant of the same subclass of polymorphism. Calling them "very different" put too much emphasis to what makes them different and not enough to what they have in common. Most of the times, having one or the other won't make a difference. May 27, 2012 at 12:16

Are they two very different concepts which I have misinterpreted into different implementations of same concept?

They do serve essentially the same purpose. It's like the difference between nails and screws: either can fasten two boards together, but they work differently.

What is the difference then?

One difference lies in which objects you can send a given message to. In C++, the compiler will complain if you try to call a method that the target object doesn't implement. In Objective-C, the compiler doesn't care -- the method that gets called when you send a message to some object isn't determined until the program is running and the message is actually sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.