From cd9f4df5d9b16b7b8924da065ca5149f1a973aa3 Mon Sep 17 00:00:00 2001 From: huihut Date: Thu, 18 Mar 2021 20:55:25 +0800 Subject: [PATCH] =?UTF-8?q?https://github.com/huihut/interview/pull/75?= =?UTF-8?q?=EF=BC=88=E5=85=B3=E4=BA=8E=E5=8A=A8=E6=80=81=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E7=9A=84=E8=A1=A5=E5=85=85=EF=BC=89=E7=9A=84=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/huihut/interview/pull/75 --- README.md | 3 ++- README_en.md | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5778d23..0d42b56 100644 --- a/README.md +++ b/README.md @@ -805,11 +805,12 @@ public: ``` #### 动态多态(运行期期/晚绑定) - * 虚函数:用 virtual 修饰成员函数,使其成为虚函数 +* 动态绑定:当使用基类的引用或指针调用一个虚函数时将发生动态绑定 **注意:** +* 可以将派生类的对象赋值给基类的指针或引用,反之不可 * 普通函数(非类成员函数)不能是虚函数 * 静态函数(static)不能是虚函数 * 构造函数不能是虚函数(因为在调用构造函数时,虚表指针并没有在对象的内存空间中,必须要构造函数调用完成后才会形成虚表指针) diff --git a/README_en.md b/README_en.md index 1b00084..dc55adb 100644 --- a/README_en.md +++ b/README_en.md @@ -811,9 +811,11 @@ public: #### Dynamic polymorphism (runtime / late binding) * 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:** +* 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 * 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)