C ++中动态分派和后期绑定有什么区别? [英] What is the difference between dynamic dispatch and late binding in C++?

查看:145
本文介绍了C ++中动态分派和后期绑定有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了关于维基百科的动态分发,但无法了解动态分发和延迟在C ++中绑定。

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

使用每个机制时

维基百科的确切报价:


动态分派与后期绑定(也称为动态绑定)不同。在选择操作的上下文中,绑定是指将名称与操作相关联的过程。调度是指在确定名称所引用的操作之后,为操作选择实现。使用动态分派,该名称可能在编译时绑定到多态操作,但是直到运行时才会选择实现(这是C ++中动态分派的工作原理)。但是,后期绑定意味着动态分派,因为在选择该名称所指向的操作之前,您无法选择要选择的多态操作的实现。

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.


推荐答案

一个相当不错的答案,实际上被纳入到一个问题的晚期或早期绑定programmers.stackexchange.com

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

简而言之,后期绑定是指eval的对象侧,动态分派是指功能侧。在后期绑定中,变量的类型是运行时的变体。在动态调度中,正在执行的函数或子程序是变量。

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.

在C ++中,我们没有真正的后期绑定,因为类型是已知的(不一定是继承层次结构,至少是一个正式的基类或接口)。

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.

我可以为后期绑定提供的最好的例子是无类型的对象在Visual Basic中。

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()

编译器实际上会将相应的执行上下文为运行时引擎执行名为 DoSomething 的方法的命名查找,如果发现与正确匹配的参数,实际执行底层调用。实际上,已知对象类型的 (它继承自 IDispatch 并支持 GetIDsOfNames(),等)。但是就语言而言,变量的类型在编译时是完全未知的,并且不知道 DoSomething 甚至是一个在运行时到达执行点之前的 obj 的方法。

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.

我不会打扰转储一个C ++虚拟接口et'al,因为我相信你已经知道它们是什么样子。我希望很明显,C ++语言根本不能做到这一点。它是强类型。它可以(并且显然)通过多态虚拟方法特性做动态分派。

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.

这篇关于C ++中动态分派和后期绑定有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆