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.
 
(4 intermediate revisions by the same user not shown)
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 << println("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.
 
=== C ===
 
In C, the mechanism behind virtual functions could be provided in the following manner:
Line 51 ⟶ 56:
 
/* 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 ⟶ 103:
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>
Line 123 ⟶ 128:
 
== Virtual destructors ==
Object-oriented languages typically manage memory allocation and de-allocation automatically when objects are created and destroyed. However, some object-oriented languages allow a custom [[Destructor (computer programming)|destructor]] method to be implemented, if desired. If the language in question uses automatic memory management, the custom destructor (generally called a finalizer in this context) that is called is certain to be the appropriate one for the object in question. For example, if an object of type Wolf that inherits Animal is created, and both have custom destructors, the one called will be the one declared in Wolf.
 
In manual memory management contexts, the situation can be more complex, particularly in relation to [[static dispatch]]. If an object of type Wolf is created but pointed to by an Animal pointer, and it is this Animal pointer type that is deleted, the destructor called may actually be the one defined for Animal and not the one for Wolf, unless the destructor is virtual. This is particularly the case with C++, where the behavior is a common source of programming errors if destructors are not virtual.