C++ 虚函数和类作用域

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 normal type-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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值