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.
- C++ 虚函数和类作用域
- #C/C++笔记#C++虚函数的作用和使用方法
- C++ 类作用域和函数成员
- 函数作用域和块作用域
- 函数作用域和作用域链
- 函数作用域和作用域链
- [C/C++]函数重载、作用域、隐藏
- Python 函数和作用域
- 函数和变量作用域
- 函数和局部作用域
- C++-作用域和生命周期
- C里strlen函数和sizeof函数的作用
- C++:类作用域
- C语言编程基础-11递归函数变量的生命周期和作用域
- 【C++】setw函数的用法和作用【原创技术】
- STL next_premutation函数的C++和C的作用。
- C语言中malloc()和free()函数的具体作用
- C语言 static和extern关键字 对函数的作用
- 为何那么容易放弃
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- C++拾遗--类成员指针
- PPT培训记录
- 缓慢变化思想6
- C++ 虚函数和类作用域
- 第一个真正项目的点点滴滴
- GitHub上整理的一些工具
- cocos2d-x v3.3 ActionInterval
- 某位清 0 范例
- Android图像处理
- Iphone适配屏幕大小
- easyui datagrid列中放入linkbutton后错行问题
- web Visual Design(1)