This https://stackoverflow.com/questions/10580/what-is-early-and-late-binding链接帮助我理解静态绑定和动态绑定之间的区别?但我很困惑
静态绑定和编译时多态性有什么区别或者没有区别.
这也产生了一个关于动态绑定和运行时多态性的疑问?
In short
静态绑定与动态绑定是关于when要运行的确切代码(即函数的地址)是已知的:在编译时、链接时(均为“静态”)、加载时或运行时(均为“动态”)。
多态性首先是关于how要运行的确切代码是已知的:为了符合多态性,它必须从type正在处理的数据。当数据的“动态类型”直到运行时才知道(通常是因为类型由运行时数据输入确定)时,必须使用动态绑定,这需要动态多态性(也称为运行时多态性;C++ 提供虚拟调度)此类机制)。在其他情况下,即使正在处理的数据类型在编译时可用,虚拟调度也是有用的 - 特别是在代码更改后最小化/消除(重新)编译时间以及调整代码“膨胀”时。无论如何,编译时又名静态多态性使用编译时已知的类型来在编译或链接时绑定(即“静态”)。
Examples
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
讨论
binding通常是指程序将函数调用解析为特定函数实现的机器代码的时间:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)