C++ 虚函数和类作用域

来源:互联网 发布:斑马旅游 知乎 编辑:程序博客网 时间:2024/05/16 07:19

15.5.4. Virtual Functions and Scope

Consider the following (artificial) collection of classes:

class Base {

public:

virtualint fcn();

};

classD1 : public Base {

public:

//hides fcn in the base; this fcn is not virtual

intfcn(int); // parameter list differs from fcn in Base

//D1 inherits definition of Base::fcn()

};

classD2 : public D1 {

public:

intfcn(int); // nonvirtual function hides D1::fcn(int)

intfcn(); // redefines virtual fcn from Base

};

Theversion of fcn in D1 does not redefine the virtual fcnfrom Base. Instead, ithides fcnfrom the base. Effectively, D1 has two functions named fcn: The class inherits a virtual named fcn from the Base and defines its own, nonvirtual member named fcn that takes an int parameter.However,the virtual from the Base cannot be called from a D1 object (or reference or pointer to D1) because that function is hidden by the definition of fcn(int).

The class D2 redefines both functions that it inherits. It redefines the virtual version of fcn originally defined in Base and the nonvirtual defined in D1.

 

Calling a Hidden Virtual through theBase Class

When we call a function through a base-type reference or pointer, the compiler looks for that function in the base class and ignores the derived classes:

Basebobj; D1 d1obj; D2 d2obj;

Base*bp1 = &bobj, *bp2 = &d1obj, *bp3 = &d2obj;

bp1->fcn();// ok: virtual call, will call Base::fcn at run time

bp2->fcn();// ok: virtual call, will call Base::fcn at run time

bp3->fcn();// ok: virtual call, will call D2::fcn at run time

 

Allthree pointers are pointers to the base type, so all three calls are resolvedby looking in Base to see if fcnis defined. It is, so the calls are legal.Next, because fcn is virtual, the compiler generates code to make the call at run time based on the actual type of the object to which the reference or pointer is bound.In the case of bp2, the underlying object is a D1. That class did not redefine the virtual version of fcn that takes no arguments. The call through bp2 is made (at run time) to the version defined in Base.

 

Key Concept: Name Lookup andInheritance

Understandinghow function calls are resolved is crucial to understanding

inheritancehierarchies in C++. The following four steps are followed:

1. Start by determining the static type of the object,reference, or pointer through which the function is called.

2.Look for the function in that class.If it is not found, look in the immediate base class and continue up the chain of classes until either the function is found or the last class is searched. If thename is not found in the class or its enclosing base classes, then the call isin error.

3.Once the name is found, do normaltype-checking to see if this call is legal given the definition that was found.

4.Assuming the call is legal, the compiler generates code. If the function is virtual and the call is through a reference or pointer, then the compiler generates code to determine which version to run based on the dynamic type of the object. Otherwise, the compiler generates code to call the function directly.

0 0
原创粉丝点击