The calling of the non-virtual member function depends on the selection of a member function at the compile time based on the type of a pointer or reference to the object. However, you can make the program wait until it actually runs before deciding what version of a particular member function to cafl^ which is called late or dynamic binding. You declare a function with the virtual keyword if you want the compiler to use dynamic binding for the specific function.
When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table or the virtual-table, contains an entry for each virtual function in the class. Each object of the class in memory includes a variable called the v-pointer, which points to the class's common virtual table. For example, the Button class has virtual functions for drag() , drop() and clickQ. There is one virtual table that is associated with the class Button. This virtual table allows the v-pointer of each of those button objects to point to objects related to the v-table only. There is a separate v-pointer to each of the virtual functions of a class.
For example, the Button v-table with three pointers are:
a pointer to Button::drag (),
a pointer to a Button drop(),
and a pointer to Button::click().
During the execution of a virtual function, the program accesses the object's v-pointer related to the v-table of a
particular class.
When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table or the virtual-table, contains an entry for each virtual function in the class. Each object of the class in memory includes a variable called the v-pointer, which points to the class's common virtual table. For example, the Button class has virtual functions for drag() , drop() and clickQ. There is one virtual table that is associated with the class Button. This virtual table allows the v-pointer of each of those button objects to point to objects related to the v-table only. There is a separate v-pointer to each of the virtual functions of a class.
For example, the Button v-table with three pointers are:
a pointer to Button::drag (),
a pointer to a Button drop(),
and a pointer to Button::click().
During the execution of a virtual function, the program accesses the object's v-pointer related to the v-table of a
particular class.
No comments:
Post a Comment