第26 - 34行:虚函数。
虚函数简单地向标准输出写一行文本。
The virtual functions simply write a line of text to standard output.
第二次,进程只是引用数据成员和虚函数。
The second time, the process simply references the data members and virtual functions.
为纯虚函数所生成的代码如列表1 中所示。
The code generated for the pure virtual function is shown in Listing 1.
虚函数是实现动态多态性的方法。
Virtual Function is the method to realize dynamic polymorphism.
这有效地隐藏了基类中的虚函数。
This effectively hides the virtual function in the base class.
是添加虚函数不是成员函数。
讨论和示例程序都只限于非静态数据成员和虚函数。
The discussion and the sample program are limited to non-static data members and virtual functions.
派生类可以有两套虚函数?
但是,在某些情况下,不可能使用虚函数。
In some situations however, the use of virtual functions is not possible.
在头文件中为该虚函数所生成的代码如列表2中所示。
The code generated for the virtual function in a header file is shown in Listing 2.
派生类继承虚拟基类的虚函数的多个重写。
The derived class inherits more than one override of a virtual function of a virtual base class.
这个例子的性能和纯虚函数表例子是相同的。
The performance of this sample and the pure vtable-based sample are exactly identical.
讨论在派生类中处理私有虚函数的方式的更改。
Discusses changes in the way private virtual functions are handled in derived classes.
这个函数是一个真正的消息句柄,不是虚函数。
This function is really a message handler, not a virtual function.
编译器用正确的函数地址初始化这些虚函数表。
The compiler initializes these tables with the correct function addresses.
首先,你应该知道什么是虚函数,以及如何使用它。
Firstly, you should know what a virtual function is, and how to use it.
如果某虚函数为私有函数,则派生类型无法访问它。
If a virtual function is private, it cannot be accessed by a derived type.
当你通过基类的指针来调用虚函数,技术也是一样的。
When you invoke calls through a pointer to the base class, the technique is the same.
这样,使用虚函数的对象每个实例占用4个额外的字节。
Thus an object that USES virtual functions occupies 4 additional bytes for each instance.
类包含或者继承一个或多个纯虚函数,这样的类就是抽象基类。
A class containing (or inheriting) one or more pure virtual functions is an abstract base class.
发出错误信息,如果有人试图构建一个明确对象或调用虚函数。
The error message is issued if someone tries to explicitly construct an object or call a virtual function.
类函数定义与基类中的虚函数同名,但参数的数目或类型不同。
A class function definition has the same name as a virtual function in a base class but not the same number or type of arguments.
所谓虚类或虚函数是指应用程序以后需要,但是目前还没写的代码。
A virtual class or function is code that the application will eventually need, but it isn't written yet.
清单1定义了三个类(A、b和c),它们有一个共同的虚函数whoami 。
Listing 1 defines three classes (a, B and c) with a common virtual function WhoAmI .
当使用虚函数的时候,编译器自动根据对象的实际类型选择正确的函数。
When we use virtual functions, the compiler automatically selects the right function according to the actual type of the object.
如果函数用作静态成员函数,则重命名该函数,以便不与基类虚函数冲突。
If the function was intended to be a static member function, rename it so it doesn't conflict with the base class virtual function.
使虚函数内联的主要原因在于类定义,主要是方便的原因,比如说成员访问函数。
The main reason for making a virtual function inline is to place its definition in the class, either for convenience or to document its behavior, e. g., for accessors and mutators.
继承层次关系的根类应该定义虚函数性质的析构函数,即使这个析构函数不做任何的事情。
The root class of an inheritance hierarchy should define a virtual destructor even if the destructor has no work to do.
继承层次关系的根类应该定义虚函数性质的析构函数,即使这个析构函数不做任何的事情。
The root class of an inheritance hierarchy should define a virtual destructor even if the destructor has no work to do.
应用推荐