Early binding: When a non virtual class member function is called, compiler places the code to call the function by symbol name. Thus the function call jumps to a location that is decided by compile time or link time.
class base
{
void funct1(void);
};
base b;
b.funct1(); //compiler call address of base::function() symbol
Late binding: When virtual function call is made through a base-class pointer, the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the right function and this is called late/dynamic binding.
class base
{
virtual void funct1(void) = 0;
};
class derived: public base
{
void funct1(void){}
};
class derived2: public base
{
void funct1(void){}
};
derived d;
base * b = d;
//See how b calls function funct1() of d in three steps
//1) get d::vtable address from b::vptr
// [value: b::vptr points to d::vtable]
//2) get b::funct1() address from b::vtable entry 0
// [value: function address= d::vtable[0]]
//3) call pointer entry 0 of vtable 0
// [ call address pointed by d::vtable[0] ]
b->funct1();
derived2 d2;
base * b = d2;
//See how b calls function funct1() of d2 in three steps
//1) get d2::vtable address from b::vptr
// [value: b::vptr points to d::vtable]
//2) get b::funct1() address from b::vtable entry 0
// [value: function address= d::vtable[0]]
//3) call pointer entry 0 of vtable 0
// [ call address pointed by d::vtable[0] ]
b->funct1();
From the above two function call we are clear that we are able to deal with d::vtable and able to call d::funct1() as well as d2::vtable and d2:funct1() accordingly with a single base pointer and the call binding of individual member function was at run time not compile time.
About our authors: Team EQA
You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.