Early and Late Binding of Functions

When you compile a C++ program, as with many other languages, each of the functions is stored at a specific address. As the program is compiled each call to a function is bound to the address of the called function. In this way, as each function is called, the program knows where in memory to divert the program flow to execute the called function. These processes are called early or static binding and sometimes compile time binding. Most non OOP languages only permit early binding. When a language only has early binding, some programs can not be written. In particular there will be OOP programs that are desirable but are impossible to write with only early binding.

C++ however has two kinds of function binding: early (static) and late (dynamic) binding. By dynamic binding (also called run time binding) is meant that the program does not know which function (i.e. which address) will be used until the program is run. You can see that dynamic binding is similar to dynamic memory allocation in that we do not know exactly what memory will be addressed until the program is executed. In OOP languages like Smalltalk, all function binding is late binding. C++ since it is a mixed type language, has both early and late binding. The major differences are that early binding is faster and requires less memory while late binding permits more freedom of action. In many cases the speed lose and the extra memory is of little significance today with today's computers and especially when compared to the gain in versatility. To achieve the goal of dynamic binding, the concept of virtual functions was introduced into C++.

The example below, used to illustrate dynamic binding, is based upon the concept of an array of pointers to functions studied earlier. When this program is compiled, a table of addresses is created. This table contains the address of each function. Instead of calling the function requested directly as is normally done, the array will be used to call an entry in the address table instead. As a result, the call is not bound by the compiler directly to the address of a particular function but to the table instead. As the program runs, the array of pointers will be dereferenced and the particular function desired will be called from the table of addresses and the call will therefore be bound at run time.

Earlier in the notes there was an example: menus2.cpp that illustrates how a menu system could be created using the keyword switch. Later when pointers to functions were considered, a new example was given which used the following arrays:

image from book

 void (*menu1[ ])()={strtclnt, expense_ldgr, sales_ldgr, pandl, eoy, quit}; void (*menu2[ ])()={expenseout, expenseedit, expenseprt, eomexpenses, quit_expenses} void (*menu3[ ])()={salesout, salesedit, salesprt, eomsales, quit_sales}; 

image from book

Each of the above is an array of pointers to functions. These pointers to functions are of type void signature and void output. The array elements listed above are functions of the same type.

The menu system illustrated in menus.cpp changed the code so that the keyword switch is no longer necessary. Instead calls like the following can be used:

image from book

 cin.get(index).get(); menu1[(index-'0')-1](); 

image from book

Notice that the input will be a char into the char variable index. The char constant '0' is then subtracted from index and then 1 is subtracted from that difference. Therefore if index was '1' (that is '1' was entered from the keyboard) then the address of the function startclnt( ) was the address selected from the table to which the program flow is diverted. If the value entered from the keyboard was '3', then the address of the function sales_ldgr( ) would be bound to the program flow as the program ran. A similar menu system has been set up for the other arrays as well. Therefore the basis of this program is an illustration of dynamically bound functions.

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net