编译时多态与静态绑定有什么区别? [英] What is the difference between compile time polymorphism and static binding?

查看:178
本文介绍了编译时多态与静态绑定有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接有助于我了解静态绑定和动态绑定之间的区别?但我感到困惑

This link helped me understand the difference between static binding and dynamic binding? but I am in a confusion that

静态绑定和编译时多态性之间有什么区别还是没有区别.

这还会对动态绑定和运行时多态性产生疑问吗?

This also creates a doubt about dynamic binding and runtime polymorphism?

推荐答案

简而言之

静态绑定vs动态绑定大约是 时,已知要运行的确切代码(即函数的地址):在compile-,link-(均为"static") ,加载或运行时(均为动态").

In short

Static vs dynamic binding is about when the exact code to run (i.e. the function's address) is known: at compile-, link- (both "static"), load-, or run-time (both "dynamic").

多态性首先是关于 方式 的确切信息:要符合多态性,必须从 类型 正在处理的数据.如果直到运行时才知道数据的动态类型"(通常是因为类型是由运行时数据输入确定的),则必须使用动态绑定,并且这需要动态多态性(又名运行时多态性; C ++提供了虚拟调度)此类别的机制).在其他情况下,即使正在编译的数据类型在编译时也可用,虚拟调度仍然有用,特别是在代码更改后最小化/消除(重新)编译时间以及调整代码膨胀"的情况下.无论如何,编译时(即静态多态)使用编译时关于类型的已知信息在编译时或链接时(即静态")进行绑定.

Polymorphism is firstly about how the exact code to run is known: to qualify as polymorphism, it's necessarily inferred from the type of data being processed. When the "dynamic type" of data is not known until run-time (often because the type is determined by run-time data inputs) dynamic binding must be used, and that requires dynamic polymorphism (aka runtime polymorphism; C++ offers the virtual dispatch mechanism in this category). There are other situations when virtual dispatch is useful even though the types of data being processed are available at compile time - particularly for minimising/eliminating (re)compile times after code changes, and for tuning code "bloat". Anyway, compile-time aka static polymorphism uses what's known about the types at compile time to bind at compile- or link- time (i.e. "statically").

struct Base { virtual void f(); void g(); };
struct Derived : Base { void f(); void g(); };

Derived d;
d.f();    // if definition's in a shared library, needs dynamic binding
          // otherwise (same translation unit, linked object, static lib)
          // compiler should optimise to static binding
          // (though functionally either would work)

Base* p = factory(data);
p->f();   // dynamic binding - if p points to a Base, use Base::f()
          //                 - if p pointer to a Derived, use Derived::f()

void some_func(const char*);  // note: no polymorphism / overloads

some_func("hello world\n");
       // if some_func is defined in...
       //  - shared / dynamic link library, binds dynamically
       //  - otherwise, static binding

std::cout << "hello world\n";  // static binding
                               // compile-time polymorphism from (operator) overloading

讨论

绑定通常是指程序将函数调用解析为特定函数实现的机器代码的时间:

Discussion

binding normally refers to the time at which the program resolves a function call to a specific function implementation's machine code:

  • 静态 表示这是在编译过程中发生的

  • static means this happens during compilation

动态 意味着在启动/运行可执行程序时会发生这种情况

dynamic means this happens when the executable program is launched/running

绑定"(和"currying")也用于描述函子参数的规定(在 Stroustrup的C ++ 11常见问题解答)

"binding" - and "currying" - are also used to describe the stipulation of arguments to functors (search for "binding" in Stroustrup's C++11 FAQ)

C ++程序动态绑定函数调用的唯一情况是:

The only situations in which C++ programs bind function calls dynamically are:

  • ,当使用动态库时,在这种情况下,绑定可以由操作系统加载程序在调用main()之前完成,或者使用dlsym(或类似的特定于OS的功能)以代码形式明确地进行,返回用于调用动态库(.so,.dll等)中的函数的函数指针.

  • when a dynamic library is used, in which case the binding may be done by the Operating Systems loader before main() is called, or explicitly in code using dlsym (or similar OS-specific function), which returns a function pointer to use to call a function found in a dynamic library (.so, .dll, ...).

在虚拟调度中,当在运行时找到虚拟成员函数时,通常是通过将指针从数据对象跟踪到记录了该函数指针的虚拟调度表中来

in virtual dispatch, when a virtual member function is found at run-time, typically by following a pointer from the data-object to a virtual dispatch table where the function pointer's recorded

当程序员明确使用函数指针时

when function pointers are used explicitly by the programmer

在其他情况下,绑定是静态的:编译器本身将jmp或调用写入特定的内存地址/偏移量(无论相对于程序计数器是绝对的还是相对的无关紧要)到其创建的对象或可执行文件中,并且在程序加载或执行期间不会被修改.

In other situations, binding is static: the compiler itself writes a jmp or call to a specific memory address/offset (whether absolute or relative to the Program Counter doesn't matter) into the object or executable it creates, and that is not modified during program loading or execution.

静态/动态绑定分类仅与多态性有一点重叠:

The static / dynamic binding classification only has a little overlap with polymorphism:

  • 虚拟调度通常使用动态绑定(但有时可以根据上述示例进行优化)和

  • virtual dispatch normally uses dynamic binding (but can sometimes be optimised as per Examples above) and

C ++中所有其他形式的多态(即重载,模板,内联宏扩展)都使用静态绑定,但是

all other forms of polymorphism in C++ (namely overloading, templates, inlined macro expansions) use static binding, but

大多数非多态代码也是如此:对不在共享/动态库中的函数的任何正常"非虚拟调用也会在编译时解决.

so does most non-polymorphic code: any "normal" non-virtual call to a function that's not in a shared/dynamic library is resolved at compile time too.

这篇关于编译时多态与静态绑定有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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