Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Virtual function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C++ === [[Image:ClassDiagram for VirtualFunction.png|400px|thumb|right|Class Diagram of Animal]] For example, a base class <code>Animal</code> could have a virtual function <code>Eat</code>. Subclass <code>Llama</code> would implement <code>Eat</code> differently than subclass <code>Wolf</code>, but one can invoke <code>Eat</code> on any class instance referred to as Animal, and get the <code>Eat</code> behavior of the specific subclass. <syntaxhighlight lang="cpp"> class Animal { public: // Intentionally not virtual: void Move() { std::cout << "This animal moves in some way" << std::endl; } 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::cout << "Llamas eat grass!" << std::endl; } }; </syntaxhighlight> This allows a programmer to process a list of objects of class <code>Animal</code>, telling each in turn to eat (by calling <code>Eat</code>), 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. In C, the mechanism behind virtual functions could be provided in the following manner: <syntaxhighlight lang="c"> #include <stdio.h> /* an object points to its class... */ struct Animal { const struct AnimalVTable *vtable; }; /* which contains the virtual function Animal.Eat */ struct AnimalVTable { void (*Eat)(struct Animal *self); // 'virtual' function }; /* Since Animal.Move is not a virtual function it is not in the structure above. */ void Move(const struct Animal *self) { printf("<Animal at %p> moved in some way\n", ( void* )(self)); } /* unlike Move, which executes Animal.Move directly, Eat 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(struct Animal *self) { const struct AnimalVTable *vtable = self->vtable; if (vtable->Eat != NULL) { (*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(struct Animal *self).' */ static void _Llama_eat(struct Animal *self) { printf("<Llama at %p> Llamas eat grass!\n", ( void* )(self)); } /* initialize class */ const struct AnimalVTable Animal = { NULL }; // base class does not implement Animal.Eat const struct AnimalVTable Llama = { _Llama_eat }; // but the derived class does int main(void) { /* init objects as instance of its class */ struct Animal animal = { &Animal }; struct Animal llama = { &Llama }; 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 } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)