Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is early (static) and late (dynamic) binding in C++?

Tags:

c++

How does early and late binding look like in C++? Can you give example?

I read that function overloading is early binding and virtual functions is late binding. I read that "early (or static) binding refers to compile time binding and late (or dynamic) binding refers to runtime binding".

like image 632
Slazer Avatar asked Aug 03 '13 17:08

Slazer


People also ask

What is static early and dynamic late binding?

Polymorphism allows an object to take multiple forms – when a method exhibits polymorphism, the compiler has to map the name of the method to the final implementation. If it's mapped at compile time, it's a static or early binding. If it's resolved at runtime, it's known as dynamic or late binding.

What is early binding and late binding in C?

If the compiler knows at the compile-time which function is called, it is called early binding. If a compiler does not know at compile-time which functions to call up until the run-time, it is called late binding.

What is static and dynamic binding in C?

Static binding happens when all information needed to call a function is available at the compile-time. Dynamic binding happens when the compiler cannot determine all information needed for a function call at compile-time.

What is static binding and late binding?

There are two types of Binding: Static and Dynamic Binding in Java. If the compiler maps the method at compile-time, it is Static Binding or early binding. And, if the method is resolved at runtime, it is Dynamic Binding or late binding.


2 Answers

You read right. The basic example can be given with:

using FuncType = int(*)(int,int); // pointer to a function                                   // taking 2 ints and returning one.  int add(int a, int b) { return a + b; } int substract(int a, int b) { return a - b; } 

Static binding is when binding is known at compile time:

int main() {     std::cout << add(4, 5) << "\n"; } 

leaves no room for a dynamic change of the operation, and thus is statically bound.

int main() {     char op = 0;     std::cin >> op;      FuncType const function = op == '+' ? &add : &substract;      std::cout << function(4, 5) << "\n"; } 

whereas here, depending on the input, one gets either 9 or -1. This is dynamically bound.

Furthermore, in object oriented languages, virtual functions can be used to dynamically bind something. A more verbose example could thus be:

struct Function {     virtual ~Function() {}     virtual int doit(int, int) const = 0; }; struct Add: Function {     virtual int doit(int a, int b) const override { return a + b; }  }; struct Substract: Function {     virtual int doit(int a, int b) const override { return a - b; }  };  int main() {     char op = 0;     std::cin >> op;      std::unique_ptr<Function> func =         op == '+' ? std::unique_ptr<Function>{new Add{}}                   : std::unique_ptr<Function>{new Substract{}};      std::cout << func->doit(4, 5) << "\n"; } 

which is semantically equivalent to the previous example... but introduces late binding by virtual function which is common in object-oriented programming.

like image 192
Matthieu M. Avatar answered Sep 21 '22 18:09

Matthieu M.


These are true of all object-oriented languages, not just C++.

Static, compile time binding is easy. There's no polymorphism involved. You know the type of the object when you write and compile and run the code. Sometimes a dog is just a dog.

Dynamic, runtime binding is where polymorphism comes from.

If you have a reference that's of parent type at compile type, you can assign a child type to it at runtime. The behavior of the reference will magically change to the appropriate type at runtime. A virtual table lookup will be done to let the runtime figure out what the dynamic type is.

like image 22
duffymo Avatar answered Sep 22 '22 18:09

duffymo