DYNAMIC BINDING


                     PREVIOUS

Binding means linking. It involves linking of function definition to a function call. If linking of function call to function definition, i.e., a place where control has to be transferred is done at compile time, it is known as static binding. When linking is delayed till run time or done during the execution of the program then this type of linking is known as dynamic binding. Which function will be called in response to a function call is find out when program executes.There are two types of binding:-

  • Static binding occurs at compile time.
  • Dynamic binding occurs at run time.

Example :-Virtual functions are an example of dynamic binding:

class Base
{ 
   public: 
     virtual void DoOperation(); 
}
class Derived: public Base 
{ 
   public:
     void DoOperation(){ cout << "We did it!" << endl; 
}
}
main 

//... 
   Base * pBase = new Derived(); 
   pBase->DoOperation(); 	//dynamic binding, uses virtual 
				  table lookup at run time.. 
//... 
}
Example :- Static binding example:
template < class Derived >
class Base 
{ 
public: 
	Base() 
{,
};
	void doOperation() 
	{ 
		return static_cast(this)->doStaticOperation(); 
	}
}; 

template < class Derived >
class MyClass : public Base < Derived > 
{ 
public: 
	doStaticOperation()
{
 cout << "We did it statically!" << endl; 
}
}








Powered by Blog - Widget
Face Upward - Widget