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

后端 未结 11 1518
臣服心动
臣服心动 2020-12-07 10:52

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

相关标签:
11条回答
  • 2020-12-07 11:42

    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.

    0 讨论(0)
  • 2020-12-07 11:42

    Binding refers to the process of associating a name with an operation.

    the main thing here is function parameters these decides which function to call at runtime

    Dispatching refers to choosing an implementation for the operation after you have decided which operation a name refers to.

    dispatch control to that according to parameter match

    http://en.wikipedia.org/wiki/Dynamic_dispatch

    hope this help you

    0 讨论(0)
  • 2020-12-07 11:47

    Dynamic dispatch is what happens when you use the virtual keyword in C++. So for example:

    struct Base
    {
        virtual int method1() { return 1; }
        virtual int method2() { return 2; } // not overridden
    };
    
    struct Derived : public Base
    {
        virtual int method1() { return 3; }
    }
    
    int main()
    {
        Base* b = new Derived;
        std::cout << b->method1() << std::endl;
    }
    

    will print 3, because the method has been dynamically dispatched. The C++ standard is very careful not to specify how exactly this happens behind the scenes, but every compiler under the sun does it in the same way. They create a table of function pointers for each polymorphic type (called the virtual table or vtable), and when you call a virtual method, the "real" method is looked up from the vtable, and that version is called. So you can imaging something like this pseudocode:

    struct BaseVTable
    {
        int (*_method1) () = &Base::method1; // real function address
        int (*_method2) () = &Base::method2;
    };
    
    struct DerivedVTable
    {  
        int (*method) () = &Derived::method1;
        int (*method2) () = &Base::method2; // not overridden
    };
    

    In this way, the compiler can be sure that a method with a particular signature exists at compile time. However, at run-time, the call might actually be dispatched via the vtable to a different function. Calls to virtual functions are a tiny bit slower than non-virtual calls, because of the extra indirection step.


    On the other hand, my understanding of the term late binding is that the function pointer is looked up by name at runtime, from a hash table or something similar. This is the way things are done in Python, JavaScript and (if memory serves) Objective-C. This makes it possible to add new methods to a class at run-time, which cannot directly be done in C++. This is particularly useful for implementing things like mixins. However, the downside is that the run-time lookup is generally considerably slower than even a virtual call in C++, and the compiler is not able to perform any compile-time type checking for the newly-added methods.

    0 讨论(0)
  • 2020-12-07 11:52

    Given that wordy Wikipedia definition I'd be tempted to classify dynamic dispatch as the late binding of C++

    struct Base {
        virtual void foo(); // Dynamic dispatch according to Wikipedia definition
        void bar();         // Static dispatch according to Wikipedia definition
    };
    

    Late binding instead, for Wikipedia, seems to mean pointer-to-member dispatch of C++

    (this->*mptr)();
    

    where the selection of what is the operation being invoked (and not just which implementation) is done at runtime.

    In C++ literature however late binding is normally used for what Wikipedia calls dynamic dispatch.

    0 讨论(0)
  • 2020-12-07 11:57

    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.

    0 讨论(0)
提交回复
热议问题