Virtual function ================ In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) polymorphism in object-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time. Many programming languages, such as Java, treat all methods as virtual by default. Some of these languages, such as Python, do not allow changing this behavior. However, others may provide modifiers that prevent methods from being overridden by derived classes (such as the final and private keywords in Java and PHP ). In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) polymorphism in object-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time. Many programming languages, such as Java, treat all methods as virtual by default. Some of these languages, such as Python, do not allow changing this behavior. However, others may provide modifiers that prevent methods from being overridden by derived classes (such as the final and private keywords in Java and PHP ). import std; class Animal { public: // Intentionally not virtual: void move() { std::println("This animal moves in some way"); } virtual void eat() = 0; }; // The class "Animal" may possess a definition for Eat if desired. class Llama : public Animal { public: // The non virtual function Move is inherited but not overridden. void eat() override { std::println("Llamas eat grass!"); } }; This allows a programmer to process a list of objects of class Animal , telling each in turn to eat (by calling eat ), without needing to know what kind of animal may be in the list, how each animal eats, or what the complete set of possible animal types might be. include // an object points to its class... typedef struct Animal { const struct AnimalVTable* vtable; } Animal; // ...which contains the virtual function Animal.eat typedef struct AnimalVTable { void (*eat)(Animal* self); // 'virtual' function } AnimalVTable; /* since Animal.move is not a virtual function, it's not in the structure above. / void move(const Animal* self) { printf(" moved in some way\n", (void*)(self)); } /* unlike move, which executes Animal.move directly, eat function cannot know which function (if any) to call at compile time. Animal.eat can only be resolved at run time when eat is called. / void eat(Animal* self) { const AnimalVTable* vtable = self->vtable; if (vtable->eat) { (*vtable->eat)(self); // execute Animal.eat } else { fprintf(stderr, "'eat' virtual method not implemented\n"); } } /* implementation of Llama.eat this is the target function to be called by 'void eat(Animal* self).' / static void Llama_eat(Animal* self) { printf(" Llamas eat grass!\n", (void*)(self)); } // initialize class const AnimalVTable Animal_vt = { NULL }; // base class does not implement Animal.Eat const AnimalVTable Llama_vt = { Llama_eat }; // but the derived class does int main(void) { // initialize objects as instance of its class Animal animal = { &Animal_vt }; Animal llama = { &Llama_vt }; move(&animal); // Animal.move move(&llama); // Llama.move eat(&animal); // cannot resolve Animal.eat so print "Not Implemented" to stderr eat(&llama); // resolves Llama.eat and executes } ==References== sv:Funktion (programmering)#Virtuell funktion REFERENCES ---------- BIBLIOGRAPHY (BibTeX) -------------------- @article{ref0, title = {Virtual function}, url = {https://encyclopegia.org/a/virtual-function} } BIBLIOGRAPHY (RIS) ------------------- TY - JOUR TI - Virtual function UR - https://encyclopegia.org/a/virtual-function ER - CITE THIS ARTICLE ---------------- Cite this article: Virtual function Derived from: https://en.wikipedia.org/w/index.php?oldid=1376529940 Licence: CC BY-SA 4.0 Snapshot: enwiki-001376529940 Permalink: /a/virtual-function