| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Virtual_method#Abstract_classes_and_pure_virtual_functions |
| Contents |
|---|
| Abstract classes and pure virtual functions |
| Behavior during construction and destruction |
| Virtual destructors |
| See also |
| References |
| Polymorphism | |
|---|---|
| Ad hoc polymorphism | |
| Parametric polymorphism | |
| Subtyping | |
|
In C++, virtual methods are declared by prepending the #include<stdio.h> // an object points to its class... typedefstructAnimal{ conststructAnimalVTable*vtable; }Animal; // ...which contains the virtual function Animal.eat typedefstructAnimalVTable{ void(*eat)(Animal*self);// 'virtual' function }AnimalVTable; /* since Animal.move is not a virtual function, it's not in the structure above. */ voidmove(constAnimal*self){ printf("<Animal at %p> moved in some way\n",(void*)(self)); } /* unlike move, which executes Animal.move directly, eat function 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(Animal*self){ constAnimalVTable*vtable=self->vtable; if(vtable->eat){ (*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(Animal* self).' */ staticvoidLlama_eat(Animal*self){ printf("<Llama at %p> Llamas eat grass!\n",(void*)(self)); } // initialize class constAnimalVTableAnimal_vt={NULL};// base class does not implement Animal.Eat constAnimalVTableLlama_vt={Llama_eat};// but the derived class does intmain(void){ // initialize objects as instance of its class Animalanimal={&Animal_vt}; Animalllama={&Llama_vt}; 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 functionseditA 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. 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 destructioneditLanguages 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. If that function is a pure virtual function, then undefined behavior occurs. 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. 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). Some design patterns, such as the Abstract Factory Pattern, actively promote this usage in languages supporting this ability. Virtual destructorseditObject-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 alsoedit
Referencesedit Learning the Java Language > Interfaces and Inheritance)". docs.oracle.com. Retrieved 2020-07-11.
|