Sunday, November 25, 2007

Letter Requesting Housing Allowance

Early Binding and Late Binding in C + + (pt.1)

In this first part I explain how the bindings in C + + and the difference between early binding and late binding, while the second part I will go deeper to discover the operation of the late binding in C + +. All material is available at http://gnix.netsons.org/esempi/binding/ .

If you navigate in the programming forum you will definitely hear about Binding Call functions. The binding is simply the term used for the connection of the call to a function with the body of the function. There are two types of binding the classic early binding where it is used by the C compiler / linker to do the binding and late binding used in C + + for the polymorphism in which the connection is done at runtime. Let's see

to grasp the differences between the two types of binding with an example. View Source . Study well this simple code. Two classes, Animal and Dog derived from animal, simply call the main function that takes as its argument to the address of an object Animal and then it invokes the method faiVerso () . Now try to compile the example:

g + +-ES1 or es1.cpp

Then run it and you will see that although the main object reference is passed a dog, this press "to animal" . The reason is simple C program, and who knows, the program does not have any information able to invoke the method faiVerso () object dog, as it has a reference to an Animal object. This is because of early binding and is the only option for those who program in C
But do not despair, the C + + introduced a solution that is called late binding and is made in the binding at runtime based on the type of object. Certainly in the case of a compiled language, the compiler does not know the type of the object will be passed to the function, but inserts the code to find and call the correct function.
The late binding is implemented in different ways in different languages \u200b\u200band compilers, yet most have the operation shown in this post. We talked about late binding, then we see an example. View Source . If you look closely you'll notice that you have added the keyword to the method faiVerso virtual pet. However, if completed, will see that the result is what we expected. But as

has made this simple keyword to perform the complex task of search for proper function? Oh again, what happens when doing late binding? What does the compiler? These are all questions that will try to answer in the next few paragraphs.

The whole mechanism of late binding is installed by the compiler when the programmer requests it (by creating virtual functions). This means that when the compiler encounters the virtual keyword, it knows that it will achieve the early binding. On the contrary, his task will be to implement the mechanisms necessary to obtain the correct function call (eg. FaiVerso function () of dog).
To install this mechanism, the compiler Classic creates a table for each class that contains virtual functions called vtable. The address of each virtual function of a class is placed in the vtable. So in every class with virtual functions, it's put a pointer called VPTR (vpointer) that points to the vtable of the class.
Now when you call a virtual function through a pointer to the base class (Animal), the compiler simply inserts the necessary instructions to load the VPTR and find the address of the virtual function properly contained in the vtable of the class (Dog). The argument being that if you are not familiar is very complex, we see a diagram to better understand.

If
look at the pattern (I hope it reads well, ev. Double-click), we left an array of references to animals called A . This array contains references to three animals (dogs, cats and Pingu) they have within them the vpointer described before pointing to the corresponding vtable. If we now recall the method faiVerso an element of the array A, the code inserted by the compiler will be able to arrive at the function contained in the vtable through VPTR contained in the Cat.

In conclusion we can say that the creation of the vtable for each class, initializing the VPTR, and the inclusion of instructions for calling a virtual function operations are carried out automatically by the compiler and that through these you can harness the power of OOP.

's the end of Part 1 of this short tutorial, in the second half when we will see that in the last lines, but in detail.

0 comments:

Post a Comment