I read everywhere that Objective-C has true dynamic binding, where as C++ has only Late binding. Unfortunately none of the books go on to explain it clearly or discuss the underlying implementation. For e.g C++ uses virtual table. How about Objective-C?
2 Answers
http://www.gnu.org/software/gnustep/resources/ObjCFun.html has a pretty good description.
Basically what dynamic binding means is that at the time that the method call is actually made, the decision is made about what method to invoke. And the method can, if you wish, be dynamically chosen at that point.
Edit: Here is a lot more detail to the best of my understanding. I do not promise that it is entirely correct, but it should be mostly right. Every object in Objective C is a struct whose first member, named isa
, is a pointer to a class. Each class is itself an object that is traditionally laid out as:
struct objc_class {
Class isa;
Class super_class;
const char *name;
long version;
long info;
long instance_size;
struct objc_ivar_list *ivars;
struct objc_method_list **methodLists;
struct objc_cache *cache;
struct objc_protocol_list *protocols;
};
At runtime, here is pseudo-code for what happens on a method lookup:
Follow isa to find the class
if implementation = class.lookup_method(method):
call implementation
else if get_implementation = class.lookup_method(forwardInvocation):
implementation = get_implementation(method)
if implementation:
call implementation
else:
raise runtime error
else:
raise runtime error
And how does that lookup_method
work?
def lookup_method (class, method):
if method in class.objc_cache:
return implementation from objc_cache
else if method in class.objc_method_list:
cache implementation from objc_method_list
return implementation
else if implementation = class.super_class.lookup_method(method):
cache implementation
return implementation
else:
return null
In response to the obvious question, yes this is much slower than C++'s virtual tables. According to benchmarks, about 1/3 of the speed. Every Objective C text immediately follows that up with the fact that in the real world, method lookup speed is almost never a bottleneck.
This is much more flexible than C's method lookups. For instance you can use forwardInvocation
to cause unrecognized methods to go to an object that you have in a variable. This kind of delegation can be done without knowing what the type of that object will be at run time, or what methods it will support. You can also add methods to classes - even at runtime if you wish - without having access to the source code. You also have rich runtime introspection on classes and methods.
The obvious flip side, that any C++ programmer will be jumping up and down about, is that you've thrown away any hope of compile time type checking.
Does that explain the differences and give you sufficient detail to understand what is going on?

- 43,296
- 3
- 59
- 88
-
can you give an example? Your answer is as vague as any text book – vinod James May 10 '11 at 05:21
-
@vinod-james: I've added a lot of detail about the actual mechanism used. – btilly May 10 '11 at 07:20
Both dynamic binding and late binding are the same,in fact. In we have static binding ,or early binding , which checks the issues which happen at compile time(errors regarding thevariables,expressions etc) and these information are stored in a v-table(virtual method table). What late binding does is that it just binds the methods with those in the v-table.