Movatterモバイル変換


[0]ホーム

URL:


Wikipedia

Virtual function

This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Virtual function" – news ·newspapers ·books ·scholar ·JSTOR
(March 2013) (Learn how and when to remove this message)

Inobject-oriented programming such as is often used inC++ andObject Pascal, avirtual function orvirtual method is an inheritable andoverridablefunction ormethod that isdispatched dynamically. Virtual functions are an important part of (runtime)polymorphism inobject-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time.

Most programming languages, such asJavaScript,PHP andPython, 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 thefinal andprivate keywords inJava[3] andPHP[4]).

Purpose

edit
Further information:Dynamic dispatch

The concept of the virtual function solves the following problem:

Inobject-oriented programming, when a derived class inherits from a base class, an object of the derived class may be referred to via apointer orreference 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 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 thevirtual 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 classAnimal could have a virtual functionEat. SubclassLlama would implementEat differently than subclassWolf, but one can invokeEat on any class instance referred to as Animal, and get theEat behavior of the specific subclass.

classAnimal{public:// Intentionally not virtual:voidMove(){std::cout<<"This animal moves in some way"<<std::endl;}virtualvoidEat()=0;};// The class "Animal" may possess a definition for Eat if desired.classLlama:publicAnimal{public:// The non virtual function Move is inherited but not overridden.voidEat()override{std::cout<<"Llamas eat grass!"<<std::endl;}};

This allows a programmer to process a list of objects of classAnimal, telling each in turn to eat (by callingEat), 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... */structAnimal{conststructAnimalVTable*vtable;};/* which contains the virtual function Animal.Eat */structAnimalVTable{void(*Eat)(structAnimal*self);// 'virtual' function};/*   Since Animal.Move is not a virtual function   it is not in the structure above.*/voidMove(conststructAnimal*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.*/voidEat(structAnimal*self){conststructAnimalVTable*vtable=*(constvoid**)(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).'*/staticvoid_Llama_eat(structAnimal*self){printf("<Llama at %p> Llamas eat grass!\n",(void*)(self));}/* initialize class */conststructAnimalVTableAnimal={NULL};// base class does not implement Animal.EatconststructAnimalVTableLlama={_Llama_eat};// but the derived class doesintmain(void){/* init objects as instance of its class */structAnimalanimal={&Animal};structAnimalllama={&Llama};Move(&animal);// Animal.MoveMove(&llama);// Llama.MoveEat(&animal);// cannot resolve Animal.Eat so print "Not Implemented" to stderrEat(&llama);// resolves Llama.Eat and executes}

Abstract classes and pure virtual functions

edit

Apure virtual function orpure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is notabstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. Asubclass of anabstract 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 classMathSymbol may provide a pure virtual functiondoOperation(), and derived classesPlus andMinus implementdoOperation() to provide concrete implementations. ImplementingdoOperation() would not make sense in theMathSymbol class, asMathSymbol is an abstract concept whose behaviour is defined solely for each given kind (subclass) ofMathSymbol. Similarly, a given subclass ofMathSymbol would not be complete without an implementation ofdoOperation().

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 aninterface - similar to what the interface keyword in Java explicitly specifies. In such a use, derived classes will supply all implementations. In such adesign pattern, the abstract class which serves as an interface will containonly pure virtual functions, but no data members or ordinary methods. In C++, using such purely abstract classes as interfaces works because C++ supportsmultiple inheritance. However, because many OOP languages do not support multiple inheritance, they often provide a separate interface mechanism. An example is theJava programming language.

Behavior during construction and destruction

edit

Languages differ in their behavior while theconstructor ordestructor 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, thenundefined 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 atcompile time orlink time. Someruntime systems will issue a pure virtual function call error when encountering a call to a pure virtual function atrun 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] Somedesign patterns, such as theAbstract 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 tostatic 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. Retrieved2020-07-11.
  2. ^"9. Classes — Python 3.9.2 documentation".docs.python.org. Retrieved2021-02-23.
  3. ^"Writing Final Classes and Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)".docs.oracle.com. Retrieved2020-07-11.
  4. ^"PHP: Final Keyword - Manual".www.php.net. Retrieved2020-07-11.
  5. ^Pure virtual destructors - cppreference.com
  6. ^"abc — Abstract Base Classes: @abc.abstractmethod"
  7. ^ab"N4659: Working Draft, Standard for Programming Language C++"(PDF).
  8. ^abChen, 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".

[8]ページ先頭

©2009-2025 Movatter.jp