Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between dynamic dispatch and late binding in C++?

Tags:

c++

oop

I've recently read about the Dynamic Dispatch on Wikipedia and couldn't understand the difference between dynamic dispatch and late binding in C++.

When each one of the mechanisms is used?

The exact quote from Wikipedia:

Dynamic dispatch is different from late binding (also known as dynamic binding). In the context of selecting an operation, binding refers to the process of associating a name with an operation. Dispatching refers to choosing an implementation for the operation after you have decided which operation a name refers to. With dynamic dispatch, the name may be bound to a polymorphic operation at compile time, but the implementation not be chosen until runtime (this is how dynamic dispatch works in C++). However, late binding does imply dynamic dispatching since you cannot choose which implementation of a polymorphic operation to select until you have selected the operation that the name refers to.

like image 735
Vardit Avatar asked Nov 25 '13 08:11

Vardit


People also ask

What is the difference between dynamic binding and late binding?

1) The static binding occurs at compile time while dynamic binding happens at runtime. 2) Since static binding happens at an early stage of the program's life cycle, it also is known as early binding. Similarly, dynamic binding is also known as late binding because it happens late when a program is actually running.

What is meant by dynamic or late binding?

Dynamic binding or late binding is the mechanism a computer program waits until runtime to bind the name of a method called to an actual subroutine. It is an alternative to early binding or static binding where this process is performed at compile-time.

What is the difference between static dispatch and dynamic dispatch?

To start things off, Static Dispatch is supported by both value types and reference types. However, Dynamic Dispatch is supported only by reference types(i.e. Class). The reason for this is that, for dynamism, or dynamic dispatch, in short, we need inheritance and our value types do not support inheritance.

Is dynamic binding late binding?

Late binding is also called dynamic binding. Late binding occurs when we make implicit or indirect function calls in our program. An example of this is using function pointers or virtual functions when using classes.


2 Answers

A fairly decent answer to this is actually incorporated into a question on late vs. early binding on programmers.stackexchange.com.

In short, late binding refers to the object-side of an eval, dynamic dispatch refers to the functional-side. In late binding the type of a variable is the variant at runtime. In dynamic-dispatch, the function or subroutine being executed is the variant.

In C++, we don't really have late binding because the type is known (not necessarily the end of the inheritance hierarchy, but at least a formal base class or interface). But we do have dynamic dispatch via virtual methods and polymorphism.

The best example I can offer for late-binding is the untyped "object" in Visual Basic. The runtime environment does all the late-binding heavy lifting for you.

Dim obj  - initialize object then.. obj.DoSomething() 

The compiler will actually code the appropriate execution context for the runtime-engine to perform a named lookup of the method called DoSomething, and if discovered with the properly matching parameters, actually execute the underlying call. In reality, something about the type of the object is known (it inherits from IDispatch and supports GetIDsOfNames(), etc). but as far as the language is concerned the type of the variable is utterly unknown at compile time, and it has no idea if DoSomething is even a method for whatever obj actually is until runtime reaches the point of execution.

I won't bother dumping a C++ virtual interface et'al, as I'm confident you already know what they look like. I hope it is obvious that the C++ language simply can't do this. It is strongly-typed. It can (and does, obviously) do dynamic dispatch via the polymorphic virtual method feature.

like image 181
WhozCraig Avatar answered Oct 08 '22 20:10

WhozCraig


The link itself explained the difference:

Dynamic dispatch is different from late binding (also known as dynamic binding). In the context of selecting an operation, binding refers to the process of associating a name with an operation. Dispatching refers to choosing an implementation for the operation after you have decided which operation a name refers to.

and

With dynamic dispatch, the name may be bound to a polymorphic operation at compile time, but the implementation not be chosen until runtime (this is how dynamic dispatch works in C++). However, late binding does imply dynamic dispatching since you cannot choose which implementation of a polymorphic operation to select until you have selected the operation that the name refers to.

But they're mostly equal in C++ you can do a dynamic dispatch by virtual functions and vtables.

C++ uses early binding and offers both dynamic and static dispatch. The default form of dispatch is static. To get dynamic dispatch you must declare a method as virtual.

like image 37
masoud Avatar answered Oct 08 '22 20:10

masoud