Virtual function: Difference between revisions

Content deleted Content added
Importing Wikidata short description: "Inheritable and overridable function or method for which dynamic dispatch is facilitated"
 
(24 intermediate revisions by 18 users not shown)
Line 2:
{{more citations needed|date=March 2013}}
{{Polymorphism}}
In [[object-oriented programming]], in languages 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]] forthat whichis [[dynamic dispatch|dispatched dynamically]] is facilitated. ThisVirtual conceptfunctions isare an important part of the (runtime) [[Polymorphism (computer science)|polymorphism]] portion ofin [[object-oriented programming]] (OOP). InThey short,allow afor virtualthe functionexecution defines aof target functionfunctions to be executed, but the targetthat mightwere not beprecisely knownidentified at compile time.
 
Most programming languages, such as [[JavaScript]], [[PHP]] and [[Python (programming language)|Python]], treat all methods as virtual by default<ref>{{Cite web|title=Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)|url=https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html|access-date=2020-07-11|website=docs.oracle.com}}</ref><ref>{{Cite web|title=9. Classes — Python 3.915.25 documentation|url=https://docs.python.org/3/tutorial/classes.html|access-date=20212025-0207-2305|website=docs.python.org}}</ref> and do not provide a modifier to change this behavior. However, some languages provide modifiers to prevent methods from being overridden by derived classes (such as the ''final'' keywordand ''private'' keywords in [[Java (programming language)|Java]]<ref>{{Cite web|title=Writing Final Classes and Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)|url=https://docs.oracle.com/javase/tutorial/java/IandI/final.html|access-date=2020-07-11|website=docs.oracle.com}}</ref> and [[PHP]]<ref>{{Cite web|title=PHP: Final Keyword - Manual|url=https://www.php.net/manual/en/language.oop5.final.php|access-date=2020-07-11|website=www.php.net}}</ref>).
 
== Purpose ==
{{furtherFurther|Dynamic dispatch}}
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 is referred to.
 
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.
 
Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled.{{Citation needed|date=October 2021}}
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 AnimalClass AnimalVTable* classvtable;
} Animal;
 
/* which contains the virtual function Animal.Eateat */
typedef struct {
struct AnimalClass {
void (*Eateat)(struct Animal* *self); // 'virtual' function
} AnimalVTable;
};
 
/*
/* Since Animal.Move is not a virtual function
itSince Animal.move is not ina the structure above.virtual */function
it is not in the structure above.
void Move(struct Animal * self)
*/
{
void move(const 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 AnimalVTable* vtable = self->vtable;
{
if (vtable->eat != NULL) {
const struct AnimalClass * class = *(const void **) self;
class(*vtable->Eateat)(self); // execute Animal.Eateat
if (class->Eat)
} else {
class->Eat(self); // execute Animal.Eat
fprintf(stderr, "Eat'eat' virtual method not implemented\n");
else
}
fprintf(stderr, "Eat not implemented\n");
}
 
/*
/* implementation of Llama.Eateat this is the target function
to be called by 'void Eat(struct Animal *).' */
static to be called by 'void _Llama_eateat(struct Animal * self).'
*/
{
static void Move_Llama_eat(struct Animal * self) {
printf("<Llama at %p> Llama'sLlamas eat grass!\n", ( void* *) (self));
}
 
/* initialize class */
const struct AnimalClassAnimalVTable Animal = {(void *)NULL 0}; // base class does not implement Animal.Eat
const struct AnimalClassAnimalVTable 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 };
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 110 ⟶ 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]] - similar to what the interface keyword in Java explicitly specifies. In such a use, derived classes will supply all implementations. In such a [[design pattern]], the abstract class which serves as an interface will contain ''only'' pure virtual functions, but no data members or ordinary methods. In C++, using such purely abstract classes as interfaces works because C++ supports [[multiple inheritance]]. However, because many OOP languages do not support multiple inheritance, they often provide a separate interface mechanism. An example is the [[Java (programming language)|Java programming language]].
 
== Behavior during construction and destruction ==
Languages differ in their behavior while the [[Constructor (computer science)|constructor]] or [[Destructor (computer science)|destructor]] of an object is running. For this reason, calling virtual functions in constructors is generally discouraged.
 
In C++, the "base" function is called. Specifically, the most derived function that is not more derived than the current constructor or destructor's class is called.<ref name="cpp-std">{{cite web|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf|title=N4659: Working Draft, Standard for Programming Language C++}}</ref>{{rp|§15.7.3}}<ref name=chen>{{cite web |title=What is __purecall? |last=Chen|first=Raymond|author-link=Raymond Chen|date=April 28, 2004 |url=https://devblogs.microsoft.com/oldnewthing/20040428-00/?p=39613}}</ref><ref>{{cite web |title=Never Call Virtual Functions during Construction or Destruction |last=Meyers|first=Scott|author-link=Scott Meyers|date=June 6, 2005 |url=http://www.artima.com/cppsource/nevercall.html}}</ref> If that function is a pure virtual function, then [[undefined behavior]] occurs.<ref>{{cite web|urlname=http://www.open"cpp-std.org"/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf>{{rp|title=N4659: Working Draft, Standard for Programming Language C++|at=§13.4.6}}<ref name=chen /ref> This is true even if the class contains an implementation for that pure virtual function, since a call to a pure virtual function must be explicitly qualified.<ref>{{cite web |title=C++ corner case: You can implement pure virtual functions in the base class |last=Chen|first=Raymond|author-link=Raymond Chen|date=October 11, 2013 |url=https://devblogs.microsoft.com/oldnewthing/20131011-00/?p=2953}}</ref> A conforming C++ implementation is not required (and generally not able) to detect indirect calls to pure virtual functions at [[compile time]] or [[link time]]. Some [[runtime system]]s will issue a pure virtual function call error when encountering a call to a pure virtual function at [[Runtime (program lifecycle phase)|run time]].
 
In Java and C#, the derived implementation is called, but some fields are not yet initialized by the derived constructor (although they are initialized to their default zero values).<ref>{{cite web |title=Joy of Programming: Calling Virtual Functions from Constructors |last=Ganesh|first=S.G.|date=August 1, 2011 |url=https://www.opensourceforu.com/2011/08/joy-of-programming-calling-virtual-functions-from-constructors/}}</ref> Some [[Design pattern (computer science)|design patterns]], such as the [[Abstract Factory Pattern]], actively promote this usage in languages supporting this ability.
 
== 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 126 ⟶ 134:
== See also ==
* [[Abstract method]]
* [[Inheritance (computerobject-oriented scienceprogramming)|Inheritance]]
* [[Superclass (computer science)|Superclass]]
* [[Virtual inheritance]]
* [[Virtual class]]
* [[Interface (object oriented programming)]]
* [[Component Object Model|Component object model]] (Microsoft's COM)
* [[Virtual method table]]