Jump to content

Virtual function: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Fixed the grammatical mistake "Llama's eat grass!" ('Llama is' eat grass!) in the C virtual function implementation.
 
(459 intermediate revisions by more than 100 users not shown)
Line 1: Line 1:
{{Short description|Inheritable and overridable function or method for which dynamic dispatch is facilitated}}
In [[object-oriented programming]] (OOP), a '''virtual function''' or '''virtual [[method (computer science)|method]]''' is a function whose behavior, by virtue of being declared "virtual," is determined by the definition of a function with the same signature furthest in the inheritance lineage of the instantiated object on which it is called. This concept is a very important part of the [[Polymorphism (computer science)|polymorphism]] portion of [[object-oriented programming]] (OOP).
{{more citations needed|date=March 2013}}
==Purpose==
{{Polymorphism}}
{{see|Dynamic dispatch}}
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]], [[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.9.2 documentation|url=https://docs.python.org/3/tutorial/classes.html|access-date=2021-02-23|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'' and ''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 ==
{{further|Dynamic dispatch}}
The concept of the virtual function solves the following problem:
The concept of the virtual function solves the following problem:


In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.
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.


The distinction between virtual and not virtual resolves this ambiguity. If the function in question is designated "virtual" in the base class then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.
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}}
Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be redefined in each derived class.


In C++, ''virtual methods'' are declared by prepending the {{Cpp|virtual}} keyword to the function's declaration in the base class. This modifier is inherited by all implementations of that method in derived classes, meaning that they can continue to over-ride each other and be late-bound. And even if methods owned by the base class call the virtual method, they will instead be calling the derived method. ''Overloading'' occurs when two or more methods in one class have the same method name but different parameters. ''Overriding'' means having two methods with the same method name and parameters. Overloading is also referred to as function matching, and overriding as dynamic function mapping.
==Example==


== Example ==
For example, a base class <code>Animal</code> could have a virtual function <code>eat</code>. Subclass <code>Fish</code> would implement <code>eat()</code> differently than subclass <code>Wolf</code>, but you can invoke <code>eat()</code> on any class instance referred to as Animal, and get the <code>eat()</code> behavior of the specific subclass.


=== C++ ===
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>), [[polymorphism (computer science)|with no knowledge of what kind]] of animal may be in the list. You also do not need to have knowledge of how each animal eats, or what the complete set of possible animal types might be.
[[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">
The following is an example in C++:
class Animal {
<source lang="cpp">
public:
#include <iostream>
// Intentionally not virtual:
using namespace std;
void Move() {

std::cout << "This animal moves in some way" << std::endl;
class Animal
}
{
virtual void Eat() = 0;
public:
virtual void eat() { cout << "I eat like a generic Animal." << endl; }
};
class Wolf : public Animal
{
public:
void eat() { cout << "I eat like a wolf!" << endl; }
};
};


// The class "Animal" may possess a definition for Eat if desired.
class Fish : public Animal
class Llama : public Animal {
{
public:
public:
// The non virtual function Move is inherited but not overridden.
void eat() { cout << "I eat like a fish!" << endl; }
void Eat() override {
};
std::cout << "Llamas eat grass!" << std::endl;
}
class OtherAnimal : public Animal
{
};
};
</syntaxhighlight>
int main()
{
Animal* anAnimal[4];


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.
anAnimal[0] = new Animal();
anAnimal[1] = new Wolf();
anAnimal[2] = new Fish();
anAnimal[3] = new OtherAnimal();
for (int i = 0; i < 4; i++) {
anAnimal[i]->eat();
}


In C, the mechanism behind virtual functions could be provided in the following manner:
for (int i = 0; i < 4; i++) {
<syntaxhighlight lang="c">
delete anAnimal[i];
#include <stdio.h>
}


/* an object points to its class... */
return 0;
struct Animal {
}
const struct AnimalVTable *vtable;
</source>
};


Output with the virtual method <code>eat</code>:
/* which contains the virtual function Animal.Eat */
struct AnimalVTable {
I eat like a generic Animal.
void (*Eat)(struct Animal *self); // 'virtual' function
I eat like a wolf!
};
I eat like a fish!
I eat like a generic Animal.

Output if <code>eat</code> were not declared as virtual:
I eat like a generic Animal.
I eat like a generic Animal.
I eat like a generic Animal.
I eat like a generic Animal.

=== Java ===
In Java, all methods are by default "virtual functions". The following is an example in Java:
<source lang="java">
public class Animal
{
public void eat() { System.out.println("I eat like a generic Animal."); }
public static void main(String[] args)
{
Animal[] anAnimal = new Animal[4];

anAnimal[0] = new Animal();
anAnimal[1] = new Wolf();
anAnimal[2] = new Fish();
anAnimal[3] = new OtherAnimal();


/*
for (int i = 0; i < 4; i++)
Since Animal.Move is not a virtual function
anAnimal[i].eat();
it is not in the structure above.
}
*/
void Move(const struct Animal *self) {
printf("<Animal at %p> moved in some way\n", ( void* )(self));
}
}
public class Wolf extends Animal
{
public void eat() { System.out.println("I eat like a wolf!"); }
}
public class Fish extends Animal
{
public void eat() { System.out.println("I eat like a fish!"); }
}
public class OtherAnimal extends Animal
{
}
</source>


/*
Output:
unlike Move, which executes Animal.Move directly,
I eat like a generic Animal.
Eat cannot know which function (if any) to call at compile time.
I eat like a wolf!
Animal.Eat can only be resolved at run time when Eat is called.
I eat like a fish!
*/
I eat like a generic Animal.
void Eat(struct Animal *self) {

const struct AnimalVTable *vtable = *( const void** )(self);
==Abstract classes and pure virtual functions==
if (vtable->Eat != NULL) {
A '''pure virtual function''' or '''pure virtual method''' is a virtual function that is required to be implemented by a derived class that is not abstract. Classes containing pure virtual methods are termed "abstract;" they cannot be instantiated directly, and a [[Subclass (computer science)|subclass]] of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a [[declaration (computer science)|declaration]] (signature) and no definition (implementation). As an example, an abstract base class "MathSymbol" may provide a pure virtual function <code>doOperation()</code>, and derived classes "Plus" and "Minus" implement <code>doOperation()</code> to provide concrete implementations. Implementing <code>doOperation()</code> would not make sense in the "MathSymbol" class as "MathSymbol" is an abstract concept whose behaviour is defined solely for each given kind (subclass) of "MathSymbol". Similarly, a given subclass of "MathSymbol" would not be complete without an implementation of
(*vtable->Eat)(self); // execute Animal.Eat
<code>doOperation()</code>. Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in C++ are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to if appropriate.
} else {

fprintf(stderr, "'Eat' virtual method not implemented\n");
Pure virtual functions are also used where the method declarations are being used to define an [[interface (computer science)|interface]] for which derived classes will supply all implementations. An abstract class serving as an interface contains only pure virtual functions, and no data members or ordinary methods. Use of purely abstract classes as interfaces works in C++ as it supports [[multiple inheritance]]. Because many OO languages do not support [[multiple inheritance]] they often provide a separate interface mechanism. This is seen in [[Java (programming language)|Java]] for example.

=== C++ ===
In [[C++]], pure virtual functions are declared using a special syntax [ ''= 0'' ] as demonstrated below.
<source lang="cpp">


class B {
virtual void a_pure_virtual_function() = 0;
};
</source>
The pure virtual function declaration provides only the prototype of the method. Although an implementation of the pure virtual function is typically not provided in an abstract class, it may be included. Every non-abstract child class is still required to override the method, but the implementation provided by the abstract class may be called in this way:

<source lang="cpp">
void Abstract::pure_virtual() {
// do something
}
class Child : Abstract {
virtual void pure_virtual(); // no longer abstract, this class may be
// instantiated.
};
void Child::pure_virtual() {
Abstract::pure_virtual(); // the implementation in the abstract class
// is executed
}
</source>

The compiler knows which method implementation to call at runtime by creating a table of pointers to all of the virtual functions in a class, called a <code>vtable</code> or [[virtual table]].

== Behavior During Construction ==
Languages differ in their behaviour while the [[Constructor_(computer science)|constructor]] of an object is running. For some languages, notably C++, the virtual dispatching mechanism is not used during construction as the invariants of derived classes are not yet set up and calling a method on such an object is risky.
In other languages (e.g. Java, C#) the most derived implementation is called even during construction. <br />
In both cases the results can be surprising to some developers and should be avoided <ref>{{cite web |title=Never Call Virtual Functions during Construction or Destruction |last=Meyers|first=Scott|date=June 6, 2005 |url=http://www.artima.com/cppsource/nevercall.html}}</ref>.

===C++===
<source lang="cpp">

#include <iostream>
#include <string>

using namespace std;

class Base {
public:
Base()
{
cout << "Constructing " << length() << endl;
}
}
}


/*
virtual int length() const { return 0; }
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 */
class Derived : public Base {
const struct AnimalVTable Animal = { NULL }; // base class does not implement Animal.Eat
string m_name;
const struct AnimalVTable Llama = { _Llama_eat }; // but the derived class does


int main(void) {
public:
/* init objects as instance of its class */
Derived(const string& name)
struct Animal animal = { &Animal };
: m_name(name.empty() ? "<default>" : name) // Member variable m_name is never empty
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>


== Abstract classes and pure virtual functions ==
virtual int length() const
A '''pure virtual function''' or '''pure virtual method''' is a virtual function that is required to be implemented by a derived class if the derived class is not [[Abstract type|abstract]]. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. A [[Subclass (computer science)|subclass]] of an [[abstract class]] can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration ([[Type signature#Method signature|signature]]) and no definition ([[Method implementation|implementation]]).
{
return m_name.size();
}
};


As an example, an abstract base class <code>MathSymbol</code> may provide a pure virtual function <code>doOperation()</code>, and derived classes <code>Plus</code> and <code>Minus</code> implement <code>doOperation()</code> to provide concrete implementations. Implementing <code>doOperation()</code> would not make sense in the <code>MathSymbol</code> class, as <code>MathSymbol</code> is an abstract concept whose behaviour is defined solely for each given kind (subclass) of <code>MathSymbol</code>. Similarly, a given subclass of <code>MathSymbol</code> would not be complete without an implementation of
int main()
<code>doOperation()</code>.
{
Derived("hi"); // Expected output: "Constructing 2"


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/enwiki/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>
return 0;
}
</source>
Output:
Constructing 0


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]].
===Java===
<source lang="java">
public class Base {
public int length() { return 0; }
public Base()
{
System.out.println("Constructing " + length());
}
static class Derived extends Base {
String name_;
public Derived(String name)
{
name_ = name != null ? name : ""; // Class invariant name_ is not null
}
public int length() { return name_.length(); } // Assume name_ is not null
}


== Behavior during construction and destruction ==
public static void main(String[] args)
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.
{
new Derived("Ooops"); // NullPointerException, Derived.name_ has not been assigned to yet
}
}
</source>


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 name="cpp-std"/>{{rp|§13.4.6}}<ref name=chen /> 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]].
== Virtual Destructors ==
Object-oriented languages typically manage memory allocation and deallocation automatically when objects are created and destroyed, however some object-oriented languages allow a custom destructor method to be implemented if desired. One such language is C++, and as illustrated in the following example, it is important for a C++ base class to have a virtual destructor to ensure that the destructor from the most derived class will always be called. In the example below having no virtual destructor, while deleting an instance of class B will correctly call destructors for both B and A if the object is deleted as an instance of B, an instance of B deleted via a pointer to its base class A will fail to call the destructor for B.


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.
<source lang="cpp">
#include <iostream>
using namespace std;

class A
{
public:
A() { }
~A() { std::cout << "Destroy A" << std::endl; }
};
class B : public A
{
public:
B() { }
~B() { std::cout << "Destroy B" << std::endl; }
};
int main()
{
A* b1 = new B;
B* b2 = new B;
delete b1; // Only ~A() is called though b1 is an instance of class B
// because ~A() is not declared virtual
delete b2; // Calls destructors ~B() and ~A()
return 0;
}
</source>


== Virtual destructors ==
Output:
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 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.
Destroy A
Destroy B
Destroy A


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.
Correctly declaring the destructor for class A as <code>virtual ~A()</code> will ensure that the destructor for class B is called in both cases with the example above.


==See also==
== See also ==
* [[Abstract method]]
*[[Inheritance (computer science)]]
* [[Inheritance (object-oriented programming)]]
*[[Superclass (computer science)]]
* [[Superclass (computer science)]]
*[[Virtual inheritance]]
* [[Virtual inheritance]]
* [[Virtual class]]
* [[Interface (object oriented programming)]]
* [[Component object model]]
* [[Virtual method table]]


==References==
==References==
{{Reflist}}
<references />
* [http://www.parashift.com/c++-faq-lite/virtual-functions.html C++ FAQ Lite] Copyright © 1991-2006, Marshall Cline.


{{DEFAULTSORT:Virtual Function}}
[[Category:C++]]
[[Category:Articles with example C++ code]]
[[Category:Method (computer programming)]]
[[Category:Object-oriented programming]]
[[Category:Object-oriented programming]]


[[sv:Funktion (programmering)#Virtuell funktion]]
[[cs:Virtuální funkce]]
[[de:Virtuelle Methode]]
[[es:Función virtual]]
[[fr:Fonction virtuelle]]
[[it:Funzione virtuale]]
[[pl:Metoda wirtualna]]
[[ro:Funcţie virtuală]]
[[ru:Виртуальный метод]]
[[sv:Virtuell funktion]]

Latest revision as of 04:08, 7 December 2024

In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) 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, PHP and Python, treat all methods as virtual by default[1][2] 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 and private keywords in Java[3] and PHP[4]).

Purpose

[edit]

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 or 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]

In C++, virtual methods are declared by prepending the virtual keyword to the function's declaration in the base class. This modifier is inherited by all implementations of that method in derived classes, meaning that they can continue to over-ride each other and be late-bound. And even if methods owned by the base class call the virtual method, they will instead be calling the derived method. Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters. Overloading is also referred to as function matching, and overriding as dynamic function mapping.

Example

[edit]

C++

[edit]
Class Diagram of Animal

For example, a base class Animal could have a virtual function Eat. Subclass Llama would implement Eat differently than subclass Wolf, but one can invoke Eat on any class instance referred to as Animal, and get the Eat behavior of the specific subclass.

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;
  }
};

This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling Eat), 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:

#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 = *( const void** )(self);
    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
}

Abstract classes and pure virtual functions

[edit]

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. A subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration (signature) and no definition (implementation).

As an example, an abstract base class MathSymbol may provide a pure virtual function doOperation(), and derived classes Plus and Minus implement doOperation() to provide concrete implementations. Implementing doOperation() would not make sense in the MathSymbol class, as MathSymbol is an abstract concept whose behaviour is defined solely for each given kind (subclass) of MathSymbol. Similarly, a given subclass of MathSymbol would not be complete without an implementation of doOperation().

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.[5][6]

Pure virtual functions can also be used where the method declarations are being used to define an 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.

Behavior during construction and destruction

[edit]

Languages differ in their behavior while the constructor or 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.[7]: §15.7.3 [8][9] If that function is a pure virtual function, then undefined behavior occurs.[7]: §13.4.6 [8] 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.[10] 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 systems will issue a pure virtual function call error when encountering a call to a pure virtual function at 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).[11] Some design patterns, such as the Abstract Factory Pattern, actively promote this usage in languages supporting this ability.

Virtual destructors

[edit]

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 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.

See also

[edit]

References

[edit]
  1. ^ "Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)". docs.oracle.com. Retrieved 2020-07-11.
  2. ^ "9. Classes — Python 3.9.2 documentation". docs.python.org. Retrieved 2021-02-23.
  3. ^ "Writing Final Classes and Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)". docs.oracle.com. Retrieved 2020-07-11.
  4. ^ "PHP: Final Keyword - Manual". www.php.net. Retrieved 2020-07-11.
  5. ^ Pure virtual destructors - cppreference.com
  6. ^ "abc — Abstract Base Classes: @abc.abstractmethod"
  7. ^ a b "N4659: Working Draft, Standard for Programming Language C++" (PDF).
  8. ^ a b Chen, Raymond (April 28, 2004). "What is __purecall?".
  9. ^ Meyers, Scott (June 6, 2005). "Never Call Virtual Functions during Construction or Destruction".
  10. ^ Chen, Raymond (October 11, 2013). "C++ corner case: You can implement pure virtual functions in the base class".
  11. ^ Ganesh, S.G. (August 1, 2011). "Joy of Programming: Calling Virtual Functions from Constructors".