虚函数
维基百科,自由的百科全书
virtual function or virtual method 是一个重要的部分属于 polymorphism 属于 object-oriented programming (OOP). 虚函数允许上一层类忽略子类继承上层类中的函数–甚至当这个子类是一个对象的类型继承从上层类 even when the derived class is cast as the type of object from which it inherits.
在OOP中当一个子类继承从父类,子类的对象会指向父类的对象,当忽略子类的函数的时候,会出现一个问题如果继承对象成为基本数据类型。当一个子对象指向的对象是一个基本数据类型,这时候是不能忽略自对象的。 The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" 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.
目录 |
[编辑] 例
For example,基本类 animal 可能有一个虚函数 eat().子类 fish() 会实现eat()不同于子类wolf(),但你可以执行 eat() 在任何类实例引用到animal类,而且让eat()影响特定的子类
This allows a programmer to process a list of objects of class Animal
, telling each in turn to eat (by calling eat()
), 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.
The following is an example in C++:
#include <iostream> class Animal { public: virtual void eat() { std::cout << "I eat like a generic Animal.\n"; } }; class Wolf : public Animal { public: void eat() { std::cout << "I eat like a wolf!\n"; } }; class Fish : public Animal { public: void eat() { std::cout << "I eat like a fish!\n"; } }; class OtherAnimal : public Animal { }; int main() { Animal *anAnimal[4]; 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(); return 0; }
output:
I eat like a generic Animal. I eat like a wolf! I eat like a fish! I eat like a generic Animal.
[编辑] Abstract classes and pure virtual functions
A pure virtual function or pure virtual method is a virtual function that has a declaration (signature), but no definition (implementation). This is often used in a base class which is a conceptual abstraction of the classes that inherit from it such that it is nonsensical to define the particular function. Such classes are referred to as "abstract" and cannot be instantiated since they have an undefined function. An example might be a base class "MathSymbol" which has a virtual function doOperation()
. You might then derive class "Plus" or "Minus" which would define doOperation()
, but defining doOperation()
wouldn't make sense in the "MathSymbol" class.
Pure virtual functions are also used where the method declarations are being used to define an interface for which derived classes will supply all implementations. An abstract class serving as an interface contains only pure virtual functions, and not any 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 for example.
[编辑] C++
In C++, virtual functions can be explicitly marked as having no implementation by using a special syntax (example: class B{virtual void apurevirtualfunction() = 0;}
), and are then called pure virtual functions. The function prototype then serves as a stub that provides only the signature of the eventual implementing method. The actual definition or definitions will be provided later as overridden functions in derived classes. (The compiler knows which method implementation to call at runtime by creating a list of pointers to all the virtual functions, called the vtable
or virtual table.)
[编辑] Virtual Destructors as a special case
Often OO languages treat virtual destructors as a special case. In the case that a destructor is virtual, instead of calling just the derived class's destructor, the base' class destructor is called as well. This is because destructors are primarily used to clean up resource allocations, and the base class which may have "private" resources which would otherwise be impossible to clean-up and would create a resource leak.
[编辑] See also
- Inheritance (computer science)
- Implementation inheritance
- Inheritance semantics
- Superclass (computer science)
- Virtual inheritancezh:虚函数