In short
Static and dynamic bindings about when the exact code to run is known (that is, the address of the function): when compiling, link (both "static"), and when loading or starting (both "dynamic").
Polymorphism, firstly, about how a well-known code is known: in order to qualify as polymorphism, it is necessarily inferred from the type of data being processed. When the “dynamic type” of data is not known before runtime (often because the type is determined by the input of the runtime data), dynamic binding must be used, and this requires dynamic polymorphism (similar to polymorphism at runtime, C ++ offers virtual dispatch mechanism in this category). There are other situations where virtual dispatching is useful even though the types of processed data are available at compile time, especially to minimize / eliminate (re) compilation time after changing the code, as well as to configure the "bloat" code. In any case, compilation time or static polymorphism uses what is known about types at compile time to bind at compile time or bind time (ie, "Statically").
Examples
struct Base { virtual void f(); void g(); }; struct Derived : Base { void f(); void g(); }; Derived d; df();
Discussion
the binding usually refers to the time when the program allows the function to be called for a specific machine code for the implementation of the function:
"binding" - and "currying" - are also used to describe the conditions of arguments to functors (search for "bindings" in Stroustrup C ++ 11 Frequently Asked Questions )
The only situations in which C ++ programs associate function calls dynamically are:
when a dynamic library is used, in which case the binding can be performed by the operating system loader before calling main() or explicitly in the code using dlsym (or a similar function specific to the OS) which returns a pointer to a function to call the function found in the dynamic library ( .so, .dll, ...).
in virtual dispatch, when a virtual member function is found at run time, usually following the pointer from the data object to the virtual dispatch table where the function pointer is written
when function pointers are explicitly used by the programmer
In other situations, the binding is static: the compiler writes jmp or a call to a specific address / memory offset (regardless of whether it is absolute or relative to the program counter) in the object or executable file that it creates and that does not change when loading or program execution.
The classification of static / dynamic binding has only a slight coincidence with polymorphism:
virtual dispatch typically uses dynamic linking (but can sometimes be optimized according to the examples above) and
all other forms of polymorphism in C ++ (namely, overloading, templates, built-in macro extensions) use static binding, but
this is what most non-polymorphic codes do: any “normal” non-virtual function call that is not part of the shared / dynamic library is also allowed at compile time.
Tony delroy
source share