博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 虚函数和类作用域
阅读量:4088 次
发布时间:2019-05-25

本文共 3061 字,大约阅读时间需要 10 分钟。

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.

转载地址:http://lyyii.baihongyu.com/

你可能感兴趣的文章
通过试题理解JavaScript
查看>>
webpack的面试题总结
查看>>
实践这一次,彻底搞懂浏览器缓存机制
查看>>
Koa2教程(常用中间件篇)
查看>>
React Hooks 完全指南
查看>>
React16常用api解析以及原理剖析
查看>>
教你发布你npm包
查看>>
nvm 和 nrm 的安装与使用
查看>>
React Hooks 一步到位
查看>>
React Redux常见问题总结
查看>>
前端 DSL 实践指南
查看>>
ReactNative: 自定义ReactNative API组件
查看>>
cookie
查看>>
总结vue知识体系之实用技巧
查看>>
PM2 入门
查看>>
掌握 TS 这些工具类型,让你开发事半功倍
查看>>
前端如何搭建一个成熟的脚手架
查看>>
Flutter ListView如何添加HeaderView和FooterView
查看>>
Flutter key
查看>>
Flutter 组件通信(父子、兄弟)
查看>>