Content deleted Content added
→Example: reworked C example a bit. |
|||
(10 intermediate revisions by 7 users not shown) | |||
Line 4:
In [[object-oriented programming]] such as is often used in [[C++]] and [[Object Pascal]], a '''virtual function''' or '''virtual method''' is an inheritable and [[Method overriding (programming)|overridable]] [[function (computer science)|function]] or [[method (computer science)|method]] that is [[dynamic dispatch|dispatched dynamically]]. Virtual functions are an important part of (runtime) [[Polymorphism (computer science)|polymorphism]] in [[object-oriented programming]] (OOP). They allow for the execution of target functions that were not precisely identified at compile time.
Most programming languages, such as [[JavaScript
== Purpose ==
{{
The concept of the virtual function solves the following problem:
In [[object-oriented programming]], when a derived class inherits from a base class, an object of the derived class may be referred to via a [[Pointer (computer programming)|pointer]] or [[Reference (computer science)|reference]] of the base class type instead of the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound (linked) either "early" (by the compiler), according to the declared type of the pointer or reference, or "late" (i.e., by the runtime system of the language), according to the actual type of the object
Virtual functions are resolved "late". If the function in question is "virtual" in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not "virtual", the method is resolved "early" and selected according to the declared type of the pointer or reference.
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>
<syntaxhighlight lang="cpp">
import std;
class Animal {
// Intentionally not virtual:
void
std::
}
virtual void
};
// The class "Animal" may possess a definition for Eat if desired.
class Llama : public Animal {
// The non virtual function Move is inherited but not overridden.
void
std::
}
};
</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>
=== 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 {
const
} Animal;
/* which contains the virtual function Animal.
typedef struct {
struct AnimalVTable {▼
void (*
/*
Since Animal.
it is not in the structure above.
*/
void
printf("<Animal at %p> moved in some way\n", (
}
/*
unlike
Eat cannot know which function (if any) to call at compile time.
Animal.
*/
void
const
if (vtable->
(*vtable->
} else {
fprintf(stderr, "'
}
}
/*
implementation of Llama.
to be called by 'void
*/
static void _Llama_eat(
printf("<Llama at %p>
}
/* initialize class */
const
const
int main(void) {
Line 98 ⟶ 103:
struct Animal animal = { &Animal };
struct Animal llama = { &Llama };
}
</syntaxhighlight>
Line 113 ⟶ 118:
Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in some languages (e.g. C++ and Python) are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to, if appropriate.<ref>[https://en.cppreference.com/w/cpp/language/destructor#Pure_virtual_destructors Pure virtual destructors - cppreference.com]</ref><ref>[https://docs.python.org/3/library/abc.html "abc — Abstract Base Classes: @abc.abstractmethod"]</ref>
Pure virtual functions can also be used where the method declarations are being used to define an [[interface (Java)|interface]]
== Behavior during construction and destruction ==
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.
Line 134 ⟶ 139:
* [[Virtual class]]
* [[Interface (object oriented programming)]]
* [[Component Object Model|Component object model]] (Microsoft's COM)
* [[Virtual method table]]
|