Sunday, July 26, 2009

Diffrence between static binding and dynamic bindig?

in c++ programming language

Diffrence between static binding and dynamic bindig?
Dynamic Versus Static Binding


Not every call of a virtual function is resolved dynamically. In fact, in many cases the compiler resolves the call statically, even if the function is declared virtual. For example:





class A


{


public:


virtual void func() {/*..*/}


};


int main()


{


A a;


A.func(); // resolved at compile time


}





Dynamic binding applies in two cases: when calling a virtual function through a pointer to a polymorphic object, and when calling a virtual function through a reference to a polymorphic object (a polymorphic object is one that declares or inherits at least one virtual member function). For example.:





void f(A %26amp; ref, A* pa)


{


ref.func(); // resolved dynamically


pa-%26gt;func(); // resolved dynamically


}





int main()


{


A a;


f(a, %26amp;a);


}





You can bypass the dynamic binding mechanism by using explicit qualification with the scope operator:





pa-%26gt;A::func(); // resolved statically





With Regards,


K.SivaKumar.


http://www.indianbabycare.com


http://www.tamilmasala.net
Reply:static binding: it means that class objects are binded/initialized at compile time. this implies that compiler has all the information about the type of the object.





dynamic binding: it means that class objects are binded/initialized at runtime, and compiler doesnt have any info. about the type of object.
Reply:static binding does not change. dynamic binding does.
Reply:stating binding is done at the compilation time and it canot change while the run time,


the Dynamic one can change it while the run time,


as the link list


No comments:

Post a Comment