Reputation: 1
Since i'm studying object oriented programming language it is quite better than C and pretty new concepts. I'm confused with early binding and late binding so my question is
What is the difference between early binding and late binding in C++?
Upvotes: 4
Views: 7550
Reputation: 1090
I would recommmend you to go through this. It perfectly explains everything you need to know http://www.learncpp.com/cpp-tutorial/124-early-binding-and-late-binding/
When a C++ program is executed, it executes sequentially, beginning at the top of main(). When a function call is encountered, the point of execution jumps to the beginning of the function being called. How does the CPU know to do this?
When a program is compiled, the compiler converts each statement in your C++ program into one or more lines of machine language. Each line of machine language is given its own unique sequential address. This is no different for functions -- when a function is encountered, it is converted into machine language and given the next available address. Thus, each function ends up with a unique address.
Binding refers to the process that is used to convert identifiers (such as variable and function names) into addresses. Although binding is used for both variables and functions, in this lesson we’re going to focus on function binding.
Upvotes: 0
Reputation: 23
Most of the function calls the compiler encounters will be direct function calls. e.g.,
int sum(int a, int b) {
return a + b;
}
int main() {
std::cout << sum(2, 3); // This is a direct function call
return 0;
}
Direct function calls can be resolved using a process known as early binding. Early binding (also called static binding) means the compiler is able to directly associate the identifier name (such as a function or variable name) with a machine address. Remember that all functions have a unique machine address. So when the compiler encounters a function call, it replaces the function call with a machine language instruction that tells the CPU to jump to the address of the function.
Late Binding
Compiler doesn't know until runtime which function to invoke.
In some programs, it's not possible to know which function will be called until runtime. This is known as late binding (or dynamic binding). In C++, one way to get late binding is to use function pointers or the other way is the use of virtual functions in inheritance. e.g.,
int add(int x, int y) {
return x + y;
}
int subtract(int x, int y) {
return x - y;
}
int main() {
int x, y;
std::cin >> x >> y;
int operation;
std::cout << "choose 0 for add & 1 for subtract\n";
std::cin >> operation;
int (*p)(int, int); // Function Pointer
// Set p to point to the function the user chose
switch (operation) {
case 0 : p = add;
break;
case 1 : p = subtract;
break;
}
// Call the function that p is pointing to
std::cout << "The answer is: " << p(x, y) << std::endl;
return 0;
}
Upvotes: 0
Reputation: 3
Basically Early binding happens at compile time also called static binding where as late binding is at run time also called dynamic binding.
In Early binding objects are nearly a strong type of objects or static type objects.Its advantage is that it is used for ease of development and its efficient performance.It reduces the number errors at run time.
Whereas in late binding methods,objects,variables are checked at run time. The most popular example for late binding is the use of reflection and use of dynamic binding.
Upvotes: 0
Reputation: 76
Let's understand it with reference to the concept of overloaded and overriden methods....so early binding also known as static binding ( compile time polymorphism ) says that the calls to the overloaded functions is resolved at the compile time i.e which version of overloaded function must be called is decided at the compile time of program execution.
Now in case of late binding also known as dynamic binding ( runtime polymorphism ) says that the calls to the overriden function is resolved at the runtime i.e which version of the overriden function must be called is decided at the runtime of program execution.
Upvotes: 0
Reputation: 24946
Consider the following code:
void foo()
{
std::cout << "A\n";
}
void bar()
{
std::cout << "B\n";
}
// returns a callable object which either refers to foo or bar
// depending on user input during runtime
std::function<void()> maker()
{
std::cout << "Please give a number:\n";
auto x = 0.0;
if (std::cin >> x && x > 50.0)
{ // if we get a valid value from the console and if it is > 50
// we return a callable that refers to foo
return{ &foo };
}
// otherwise we return a callable that refers to bar
return{ &bar };
}
A simple call to foo
or bar
can be resolved at compile time (the function binding can happen early/during compilation) but using maker()
will result in late binding because it is actually unknown to the compiler which function will be called:
auto f = maker();
f(); // whether foo or bar is called depends
// on the user input during maker execution
The same is true for all other methods of dynamic dispatching (i.e. virtual function calls).
If the compiler is not able to prove (at compile time) that (indepedant of the runtime information) a specific function is to be called at a certain point of the program flow, late function binding is used to resolve the appropriate action at runtime. Otherwise early function binding is used to resolve the call at compile time (which may not necessarily be an actual call but can also be done via inlining).
-O0
for both, foo
and bar
while from -O2
the assembly output show a
call t_deriv_1::f() const
in the assembly of bar
because make_bar
is known when bar
is compiled and is examined to find that the type which is actually returned is always t_deriv_1
.
struct t_base
{
virtual int f() const = 0;
virtual ~t_base() {}
};
struct t_deriv_1 : t_base
{
int f() const override final;
};
struct t_deriv_2 : t_base
{
int f() const override final;
};
t_base * make_foo();
t_base * make_bar()
{
return new t_deriv_1;
}
void foo()
{
t_base * p = make_foo();
p->f();
delete p;
}
void bar()
{
t_base * p = make_bar();
p->f();
delete p;
}
Upvotes: 5
Reputation: 1385
Let us consider an example
int add(int a, int b){
return a + b;
}
int main(){
int sum = add(1, 2);
return 0;
}
Here it is early binding, because compiler could directly know the address of add
and uses it in the function call.
Late binding can occur in two cases
- Pointer to a function
int add(int a, int b){
return a + b;
}
int sub(int a, int b){
return a - b;
}
int main(){
int(*p)(int, int);
char op; //either `+` or `-`
cin >> op;
p = (op == '+')?add:sub;
int result = p(10, 5);
return 0;
}
Here the function call is intermediated by a function pointer p
which is based on op
at run time, so the compiler cannot know the address while compiling, the address is known while running (runtime). It s late binding.
- A method of a derived object is called using a base class pointer
class Base
{
public:
void x() {cout << "base ";}
virtual void y() {cout << "base ";}
};
class Derived : public Base
{
void x() {cout << "derived ";}
virtual void y() {cout << "derived ";}
};
int main()
{
Base* b = new Derived;
b->x();
b->y();
}
Here the output will be
base derived
b->x()
invokes the bass class function because it is not virtual
, but b->y()
invokes the derived class function because it is virtual
. Here the look up takes up with the intermediate help of vtables
while running (runtime). So it is late binding.
Rule of thumb: 1. Whatever can be evaluated by the compiler and substitutes at compile time are early binding 2. Whatever can't be evaluated by the compiler at compile time and only could be evaluated at run time are late binding
Upvotes: 2
Reputation: 310909
'Early binding' applies to method overloads. The compiler can determine which of the available overloads is applicable, based on the types of the arguments.
'Late binding' applies to method overrides. The compiler cannot determine which override is applicable, as it depends on the runtime type of the object the method is being invoked on. So a runtime despatch via a virtual-function-table mechanism or similar is required.
Upvotes: 3
Reputation: 1
Early binding is basically a concept of object programming language where what type of object is known at compile time and if don't know about type of object at compile time that phenomenon is known as late binding and run time binding.
Upvotes: 0
Reputation: 563
During early binding the compiler can resolve exactly which function will be called and therefore use the functions address for the function call.
During late binding the compiler can not deduce the exact function which will be called, usually because of polymorphsim. And therefore the function call is resolved during runtime.
Upvotes: 3