huihut 2021-03-18 20:55:25 +08:00
parent f279fe87ca
commit cd9f4df5d9
2 changed files with 4 additions and 1 deletions

View File

@ -805,11 +805,12 @@ public:
``` ```
#### 动态多态(运行期期/晚绑定) #### 动态多态(运行期期/晚绑定)
* 虚函数:用 virtual 修饰成员函数,使其成为虚函数 * 虚函数:用 virtual 修饰成员函数,使其成为虚函数
* 动态绑定:当使用基类的引用或指针调用一个虚函数时将发生动态绑定
**注意:** **注意:**
* 可以将派生类的对象赋值给基类的指针或引用,反之不可
* 普通函数(非类成员函数)不能是虚函数 * 普通函数(非类成员函数)不能是虚函数
* 静态函数static不能是虚函数 * 静态函数static不能是虚函数
* 构造函数不能是虚函数(因为在调用构造函数时,虚表指针并没有在对象的内存空间中,必须要构造函数调用完成后才会形成虚表指针) * 构造函数不能是虚函数(因为在调用构造函数时,虚表指针并没有在对象的内存空间中,必须要构造函数调用完成后才会形成虚表指针)

View File

@ -811,9 +811,11 @@ public:
#### Dynamic polymorphism (runtime / late binding) #### Dynamic polymorphism (runtime / late binding)
* Virtual functions: decorate member functions with virtual to make them virtual * Virtual functions: decorate member functions with virtual to make them virtual
* Dynamic binding: dynamic binding occurs when a virtual function is called using a reference or pointer to a base class
**note:** **note:**
* You can assign an object of a derived class to a pointer or reference of the base class, and not vice versa
* Ordinary functions (non-class member functions) cannot be virtual functions * Ordinary functions (non-class member functions) cannot be virtual functions
* Static functions (static) cannot be virtual functions * Static functions (static) cannot be virtual functions
* The constructor cannot be a virtual function (because when the constructor is called, the virtual table pointer is not in the object's memory space, the virtual table pointer must be formed after the constructor is called) * The constructor cannot be a virtual function (because when the constructor is called, the virtual table pointer is not in the object's memory space, the virtual table pointer must be formed after the constructor is called)