0

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.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
stantonk
  • 1,922
  • 1
  • 18
  • 24
  • 1
    I don't think this book is saying that C++ doesn't support dynamic dispatch. I think it's saying that in C++, the dispatch table (used for dynamic dispatch) is constructed at compile-time (i.e. "statically"). That is true, at least for many common implementations of C++. I don't know anything about Objective-C, though, so I can't compare this to what Objective-C does. – James McNellis Jan 02 '12 at 20:40
  • 1
    I guess it depends on how you define these. I think most people would say that C++ has "dynamic dispatch", although most implementations won't need to do any runtime searching to achieve this. – Oliver Charlesworth Jan 02 '12 at 20:40
  • I have added the `objective-c` tag. The discussion would probably benefit from people knowledgeable in `objective-c` – David Rodríguez - dribeas Jan 02 '12 at 23:17

6 Answers6

2

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.
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

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.

peak
  • 311
  • 3
  • 6
1

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++.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

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."

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • The book seems to clearly state that C++ does **not** support "Dynamic Dispatch", which the book seems to define as being able to determine, at runtime, which method to dispatch in response to a message by searching the receiver and it's parent classes. I think the point of confusion perhaps is the fact that in Objective-C, the recipient class of a message might not have a method to dispatch in response at one point during the running lifecycle of an application, but **later on** in the same run of the application, a new method may have been added to that target class, allowing it to respond. – stantonk Jan 03 '12 at 20:44
  • The problem seems to be that the use of the term "Dynamic" itself is being overloaded :-P. Since C++ constructs dispatch tables **statically**, then how can it support **dynamic** dispatch...the dispatch tables are **static**. Objective-C allows dispatch tables to be modified at **runtime**. Am I missing something? – stantonk Jan 03 '12 at 20:46
  • Unless you're talking about a portion of the book you didn't quote, the book *itself* never uses the term "dynamic dispatch". It explains very clearly the behaviors of ObjectiveC and C++ very clearly, but it never uses the term that you asked about. – Nicol Bolas Jan 03 '12 at 20:55
  • As to how C++ can have "dynamic dispatch", it all depends on how you define the term. Since neither you nor the book defined it, it can be defined using any of the various definitions. C++ allows, via polymorphism, derived classes to be used as base classes. Since a piece of code can not *statically* know which method a virtual function call will call (it could call the base class implementation or any derived class implementation), it is a *dynamic* call. Hence: dynamic dispatch. – Nicol Bolas Jan 03 '12 at 20:57
1

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.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
0

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.

aeskr
  • 3,436
  • 1
  • 15
  • 10