What are virtual functions? How does the program know to reach the correct function when a virtual function is called?
Anónimo
Virtual functions This is the tool with which (and with the help of inheritance) polymorphism is possible in C++. The function that needs to be executed is not known at compile time, because it depends on the type of object through which the function is called. The compiler generates for each class a table, which contains the addresses of the virtual functions of that class. Each object receives the address of the table of the class to which it belongs, when it is created (at runtime). For each call to a virtual function, the compiler generates code to jump to the address listed in the object's table.