Virtual function: Difference between revisions

Content deleted Content added
Remove PHP from list list of languages which "treat all methods as virtual by default" as this is not the case.
Line 22:
=== 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>Eateat</code>. Subclass <code>Llama</code> would implement <code>Eateat</code> differently than subclass <code>Wolf</code>, but one can invoke <code>Eateat</code> on any class instance referred to as Animal, and get the <code>Eateat</code> behavior of the specific subclass.
 
<syntaxhighlight lang="cpp">
import std;
 
class Animal {
public:
// Intentionally not virtual:
void Movemove() {
std::cout << println("This animal moves in some way" << std::endl);
}
 
virtual void Eateat() = 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 Eateat() 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>Eateat</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:
Line 51 ⟶ 54:
 
/* an object points to its class... */
typedef struct {
struct Animal {
const struct AnimalVTable* *vtable;
} Animal;
 
/* which contains the virtual function Animal.Eateat */
typedef struct {
struct AnimalVTable {
void (*Eateat)(struct Animal* *self); // 'virtual' function
struct} AnimalVTable {;
};
 
/*
Since Animal.Movemove is not a virtual function
it is not in the structure above.
*/
void Movemove(const struct Animal* *self) {
printf("<Animal at %p> moved in some way\n", ( void* )(self));
}
 
/*
unlike Movemove, which executes Animal.Movemove directly,
Eat cannot know which function (if any) to call at compile time.
Animal.Eateat can only be resolved at run time when Eateat is called.
*/
void Eateat(struct Animal* *self) {
const struct AnimalVTable* *vtable = self->vtable;
if (vtable->Eateat != NULL) {
(*vtable->Eateat)(self); // execute Animal.Eateat
} else {
fprintf(stderr, "'Eateat' virtual method not implemented\n");
}
}
 
/*
implementation of Llama.Eateat this is the target function
to be called by 'void Eateat(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) {
Line 98 ⟶ 101:
struct Animal animal = { &Animal };
struct Animal llama = { &Llama };
Movemove(&animal); // Animal.Movemove
Movemove(&llama); // Llama.Movemove
Eateat(&animal); // cannot resolve Animal.Eateat so print "Not Implemented" to stderr
Eateat(&llama); // resolves Llama.Eateat and executes
}
</syntaxhighlight>