In this chapter and the next, we are going to take a closer look at how virtual functions are implemented. While this information is not strictly necessary to effectively use virtual functions, it is interesting. Nevertheless, you can consider both sections optional reading.
When a C++ program is executed, it executes sequentially, beginning at the top of main(). When a function call is encountered, the point of execution jumps to the beginning of the function being called. How does the CPU know to do this?
When a program is compiled, the compiler converts each statement in your C++ program into one or more lines of machine language. Each line of machine language is given it’s own unique sequential address. This is no different for functions — when a function is encountered, it is converted into machine language and given the next available address. Thus, each function ends up with a unique machine language address.
Binding refers to the process that is used to convert identifiers (such as variable and function names) into machine language addresses. Although binding is used for both variables and functions, in this lesson we’re going to focus on function binding.
Early binding
Most of the function calls the compiler encounters will be direct function calls. A direct function call is a statement that directly calls a function. For example:
#include <iostream>
void PrintValue(int nValue)
{
std::cout << nValue;
}
int main()
{
PrintValue(5); // This is a direct function call
return 0;
}
Direct function calls can be resolved using a process known as early binding. Early binding (also called static binding) means the compiler is able to directly associate the identifier name (such as a function or variable name) with a machine address. Remember that all functions have a unique machine address. So when the compiler encounters a function call, it replaces the function call with a machine language instruction that tells the CPU to jump to the address of the function.
Let’s take a look at a simple calculator program that uses early binding:
#include <iostream>
using namespace std;
int Add(int nX, int nY)
{
return nX + nY;
}
int Subtract(int nX, int nY)
{
return nX - nY;
}
int Multiply(int nX, int nY)
{
return nX * nY;
}
int main()
{
int nX;
cout << "Enter a number: ";
cin >> nX;
int nY;
cout << "Enter another number: ";
cin >> nY;
int nOperation;
do
{
cout << "Enter an operation (0=add, 1=subtract, 2=multiply): ";
cin >> nOperation;
} while (nOperation < 0 || nOperation > 2);
int nResult = 0;
switch (nOperation)
{
case 0: nResult = Add(nX, nY); break;
case 1: nResult = Subtract(nX, nY); break;
case 2: nResult = Multiply(nX, nY); break;
}
cout << "The answer is: " << nResult << endl;
return 0;
}
Because Add(), Subtract(), and Multiply() are all direct function calls, the compiler will use early binding to resolve the Add(), Subtract(), and Multiply() function calls. The compiler will replace the Add() function call with an instruction that tells the CPU to jump to the address of the Add() function. The same holds true for for Subtract() and Multiply().
Late Binding
In some programs, it is not possible to know which function will be called until runtime (when the program is run). This is known as late binding (or dynamic binding). In C++, one way to get late binding is to use function pointers. To review function pointers briefly, a function pointer is a type of pointer that points to a function instead of a variable. The function that a function pointer points to can be called by using the function call operator (()) on the pointer.
For example, the following code calls the Add() function:
int Add(int nX, int nY)
{
return nX + nY;
}
int main()
{
// Create a function pointer and make it point to the Add function
int (*pFcn)(int, int) = Add;
cout << pFcn(5, 3) << endl; // add 5 + 3
return 0;
}
Calling a function via a function pointer is also known as an indirect function call. The following calculator program is functionally identical to the calculator example above, except it uses a function pointer instead of a direct function call:
#include <iostream>
using namespace std;
int Add(int nX, int nY)
{
return nX + nY;
}
int Subtract(int nX, int nY)
{
return nX - nY;
}
int Multiply(int nX, int nY)
{
return nX * nY;
}
int main()
{
int nX;
cout << "Enter a number: ";
cin >> nX;
int nY;
cout << "Enter another number: ";
cin >> nY;
int nOperation;
do
{
cout << "Enter an operation (0=add, 1=subtract, 2=multiply): ";
cin >> nOperation;
} while (nOperation < 0 || nOperation > 2);
// Create a function pointer named pFcn (yes, the syntax is ugly)
int (*pFcn)(int, int);
// Set pFcn to point to the function the user chose
switch (nOperation)
{
case 0: pFcn = Add; break;
case 1: pFcn = Subtract; break;
case 2: pFcn = Multiply; break;
}
// Call the function that pFcn is pointing to with nX and nY as parameters
cout << "The answer is: " << pFcn(nX, nY) << endl;
return 0;
}
In this example, instead of calling the Add(), Subtract(), or Multiply() function directly, we’ve instead set pFcn to point at the function we wish to call. Then we call the function through the pointer. The compiler is unable to use early binding to resolve the function call pFcn(nX, nY) because it can not tell which function pFcn will be pointing to at compile time!
Late binding is slightly less efficient since it involves an extra level of indirection. With early binding, the compiler can tell the CPU to jump directly to the function’s address. With late binding, the program has to read the address held in the pointer and then jump to that address. This involves one extra step, making it slightly slower. However, the advantage of late binding is that it is more flexible than early binding, because decisions about what function to call do not need to be made until run time.
In the next lesson, we’ll take a look at how late binding is used to implement virtual functions.
12.5 — The virtual table
|
Index
|
12.3 — Virtual destructors, virtual assignment, and overriding virtualization
|
12.5 — The virtual table
Index
12.3 — Virtual destructors, virtual assignment, and overriding virtualization
[...] 2007 Prev/Next Posts « 12.2 — Virtual functions | Home | 12.4 — Early binding and late binding » Friday, February 1st, 2008 at 1:50 [...]
[...] 2007 Prev/Next Posts « 12.4 — Early binding and late binding | Home | 12.6 — Pure virtual functions, abstract base classes, and interface classes » [...]
Good example.. Easy to understand
very good example
Vary good example….
Why do you use the “&” in the first example
// Create a function pointer and make it point to the Add function int (*pFcn)(int, int) = &Add;but not in the second example?
int (*pFcn)(int, int); switch (nOperation) { case 0: pFcn = Add; break;It actually works either way (as far as I know). I changed the examples to make them consistent though.
very clear
that’s bullshit… it doesn’t work… !! do you know programming at all????? !!!!
Nice and clear thank you.
the piece of code is an example of late binding itself. Put a code of Virtual functions. Virtual functions are perfect example of late binding or runtime polymorphism.
these lines are clearly late binding example….
switch (nOperation) { case 0: pFcn = Add; break; case 1: pFcn = Subtract; break; case 2: pFcn = Multiply; break; }it is good example for C++ late binding and early binding
[...] 12.4 Early binding and late binding [...]
Nice explanation, very easy to understand , I have already saved the web address.
Woooohoooooo ALEX, Till now nobody cleared my doubt regarding even my college professors !!
Thank you Alex for your mind blowing session !!
Hi Alex,
In the tutorial you said that “The compiler is unable to use early binding to resolve the function call pFcn(nX, nY) because it can not tell which function pFcn will be pointing to at compile time!”
I think the reason for late binding is that the allocation of function pointer pFcn(nX,nY) is at run time(as it is allocated from heap) and hence the function call cannot be resolved at compile time.
If I change the code to below ::
switch (nOperation)
{
case 0: Add(nX,nY); break;
case 1: Subtract(nX,nY); break;
case 2: Multiply(nX,nY); break;
}
Will this be late binding or early binding ??
I feel this is early binding as compiler has the address of the functions and the control can move to the address based on choice of user at run time.
Kindly suggest on the above.
Thanks
Rahul
[...] and late binding The 2nd link addresses the VTable which is related to early and late binding Early and Late binding The vtable or virtual [...]
Nice explanation, very easy to understand.. But my personal opinion is that function pointers are quite interesting than conventional programming. Function Pointers provide some extremely interesting, e?cient and elegant programming techniques. You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks.
“The same holds true for for Subtract() and Multiply().”