Question

On page 4, it says:

Objective-C decides dynamically--at run-time--what code will handle a message by searching the receiver's class and parent classes. (The Objective-C runtime caches the search results for better performance.) By contrast, a C++ compiler constructs a dispatch table statically -- at compile time.

I've read a lot on StackOverflow and Wikipedia, and suffice it to say I'm utterly confused as to whether or not C++ supports Dynamic Dispatch (which some say is an implementation of Dynamic Binding).

Anyone able to clear up the difference between Dynamic Dispatch, Dynamic Binding, and whether or not C++ supports one of or both of those? I'm not a C++ or Objective-C expert, I'm coming from a Java, Python and PHP world.

Was it helpful?

Solution

The dynamic dispatch referred to in this book is probably a different dynamic dispatch typically referred to in thr C++ context:

  • C++ support dynamic dispatch in the form of virtual functions. The corresponding names and parameters are indeed known at compile time although the actual finction called depends on the dynamic type of the object.
  • I'm not an Objective C expert but my understanding is that you can dynamically add functions to individual objects at run-time which are looked up when being called. C++ doesn't support this sort of dynamic dispatch.

OTHER TIPS

C++ does support dynamic dispatch, via virtual member functions.

I don't think this book is saying otherwise. It states that "a C++ compiler constructs a dispatch table statically -- at compile time." This is true: the dispatch tables ("vtable") that are used to implement dynamic dispatch are constructed at compile-time, at least in most common implementations of C++.

Your title is different from your question.

The statement from the book is correct: C++ virtual dispatch is executed at runtime, but the dispatch tables are compile-time generated. However, that's different from saying that C++ does not support "dynamic dispatch". Virtual functions are a form of dynamic dispatches, but there are many levels of things that are filed under the term "dynamic dispatch."

If "dynamic dispatch" means "change at runtime which function is invoked for a call to a method, for a given object", than yes: C++ doesn't have -at language level- a structured native mechanism to do this (it means change at runtime a v-table pointer, or even a function pointer inside a v-table: it is possible by forcing implementation specific constructs, but may hurt children :-) treat it as "porno coding"!)

But C++ has a "dynamic dispatch" based on class inheritance and virtual function. You can come to the most possible dynamic dispatch by implementing an object as an aggregate of subobjets, implementing their own variant for a given interface (in substance, the "behavior pattern"), and changing a sub-object when needed.

Basically, C++ is 'partial' dynamic by using the keyword 'virtual'. we would usually refer this feature as 'late method binding', which decides the specific method to call at runtime.

OC however is 'pure' dynamic(not as pure as javascript) by its powerful runtime system. you can add methods and ivars at runtime, not to mention choosing the right method to call at runtime. we usually refer this feature as 'dynamic message dispatching'.

As you can see, from the method calling perspective, they almost function the same. the detailed method searching process differs(c++ looks into the virtual table, oc try its luck in its class's method list), but they both get their runtime flexibility.

C++ does support dynamic dispatch via virtual functions.

However, it does not (natively) support double dispatch, which is a system which determines method to call based on the runtime type of the object and the runtime type of the method's parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top