virtual function specifier
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
指定一个函数是虚拟的
目录 |
[编辑] 语法
virtual function_declaration ;
|
|||||||||
[编辑] 解释
虚函数的成员函数的行为,可以在派生类中重写。相对于非虚函数,重写的行为被保留,即使是没有编译时的实际类型之类的信息。这意味着,即使处理一个派生类的基类的指针或引用,调用一个重载的虚函数调用在派生类中定义的行为.
原文:
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. That means, even if a derived class is handled using pointer or reference to the base class, a call to a overridden virtual function would invoke the behavior defined in the derived class.
以被覆盖的函数签名必须是相同的。
本章尚未完成 原因:handling of function signatures (member lookup rules) |
原文:
The function signature must be the same in order to be overridden.
本章尚未完成 原因:handling of function signatures (member lookup rules) |
一个压倒一切的虚函数的返回类型,并不一定需要是相同的覆盖功能。类型可以是不同的,如果他们是“协变”与彼此。两种类型是协变的,如果他们满足下列要求:
原文:
The return type of a overriding virtual function doesn't necessarily need to be the identical to that of the overridden function. The types can be different if they are covariant with each another. Two types are covariant if they satisfy the following requirements:
- 在这里,我们所说的最重要的功能
Derived::f()
和重写功能Base::f()
原文:Here we refer to the overriding function asDerived::f()
and to the overridden function asBase::f()
- 这两种类型的指针或引用的类。不允许多层次的指针或引用.原文:both types are pointers or references to classes. Multi-level pointers or references are not allowed.
-
Base::f()
的返回类型之类的必须是明确的和可访问的直接或间接基类的类的返回类型Derived::f()
.原文:the class of the return type ofBase::f()
must be a unambiguous and accessible direct or indirect base class of the class of the return type ofDerived::f()
.
如果直接调用虚函数,也就是说,明确限定类中的一员,那么虚拟调用机制被抑制,特别是实施被称为.
原文:
If a virtual function is called directly, that is, explicitly qualifying the class it is a member of, then the virtual call mechanism is suppressed and that particular implementation is called.
虚析构函数总是覆盖的基类的派生类的析构函数,析构函数,否则即使不继承.
原文:
A virtual destructor of a base class is always overridden by a destructor of a derived class, even though that destructors are otherwise not inherited.
访问规则是由一个虚函数的第一个声明。的声明最重要的功能定义的访问规则只适用于直接的函数调用.
原文:
The access rules to a virtual function are determined by the first declaration. Access rules defined by the declarations of the overriding functions apply only to the direct function calls.
virtual
功能说明暗示的成员,因此,只有成员函数可以是虚的。此外,由于一类的一个实例是必要的,调用虚函数,虚函数不能static.原文:
virtual
function specifier implies membership, thus only member functions can be virtual. Also, since an instance of a class is needed in order to call a virtual function, virtual function can not be static.函数模板不能宣布
virtual
。这仅适用于函数本身是模板 - 一个普通的类模板成员函数被声明为虚.原文:
Functions templates cannot be declared
virtual
. This applies only to functions that are themselves templates - a regular member function of a class template can be declared virtual.[编辑] 示例
class Parent { public: void functionA(); virtual void functionB(); //Note the keyword virtual void functionC(); }; class Child : public Parent { public: void functionA(); virtual void functionB(); //Note the keyword virtual }; int main() { Parent* p1 = new Parent; Parent* p2 = new Child; Child* c = new Child; p1->functionA(); //Calls Parent::functionA p1->functionB(); //Calls Parent::functionB p1->functionC(); //Calls Parent::functionC p2->functionA(); //Calls Parent::functionA because p2 points to a Parent p2->functionB(); //Calls Child::functionB even though p2 points // to a Parent because functionB is virtual p2->functionC(); //Calls Parent::functionC c->functionA(); //Calls Child::functionA c->functionB(); //Calls Child::functionB c->functionC(); //Calls Parent::functionC return 0; }