Static binding vs Dynamic binding
Probably most of us, studied the Static/Dynamic binding in compiler design course in Engineering. For me i did not understand that time ;-), though i understand now and shall explain it with an example so that guys with similar IQs like me can understand before coming out of college :-) .
Lets try an example of C++ program.
Class A{
public:
virtual void foo(){ /* ... */
};
int main()
{
A a;
a.foo(); --->> What do you think??
return 0;
}
During compilation, compiler has enough knowledge about the data type "A" and it's object 'a'. Hence when you write compiler can readily identify the foo function during compilation time and thus binds it with object 'a'. This is called static binding.
-- So what's the Dynamic binding??
Lets consider the code with some modification.
Void foo1(A &ref){
// ref.foo(); --> Is this something different from static binding??
}
Void foo1(A *ref){
// ref->foo(); --> Is this something different from static binding
}
int main()
{
A a;
foo1(a);
fooe1(&a);
return 0;
}
So during compilation time the function body is not executed. It is executed during runtime. Hence when we are specifying &a or *a , the object type is determined during runtime. Thus calling a the function 'foo' is fully depends on runtime. Thus it is dynamic binding.
Do let me know if you have any query. ;-)
Lets try an example of C++ program.
Class A{
public:
virtual void foo(){ /* ... */
};
int main()
{
A a;
a.foo(); --->> What do you think??
return 0;
}
During compilation, compiler has enough knowledge about the data type "A" and it's object 'a'. Hence when you write
-- So what's the Dynamic binding??
Lets consider the code with some modification.
Void foo1(A &ref){
// ref.foo(); --> Is this something different from static binding??
}
Void foo1(A *ref){
// ref->foo(); --> Is this something different from static binding
}
int main()
{
A a;
foo1(a);
fooe1(&a);
return 0;
}
So during compilation time the function body is not executed. It is executed during runtime. Hence when we are specifying &a or *a , the object type is determined during runtime. Thus calling a the function 'foo' is fully depends on runtime. Thus it is dynamic binding.
Do let me know if you have any query. ;-)
Labels: Technical Details

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home