tags:

views:

68

answers:

1

What are the two ways that are used to implement dynamic binding in OO languages. Like how is dynamic binding implemented for a pure OOL like Smalltalk versus mixed OOL such as C++?

+1  A: 

I don't know if there are two ways, and I don't know if it's pure vs. mixed OO languages.

Basically, C++ uses virtual table dispatch - each instance carries a table of functions (in case of C++, it carries a pointer to the table, but that doesn't play any role in the dispatch mechanism), and when you call x.my_method(), the compiler knows its eg. the second method of an object, so it emits code to jump to the second pointer in the virtual method table.

In dynamic languages (and ObjC), it's usually solved by having a dictionary of methods somewhere, where, on runtime, the name my_method is looked up and whatever function is found, is executed.

There are mixed approaches - in COM, you look up the interface by indentifier; then you execute a method from its vtable.

Sometimes, code like some switch statement (eg. switch depending on the type of an object) is generated to speed up the second approach.

jpalecek