|
| 1 | +# C++面试冲刺笔记1:虚函数的基本工作原理 |
| 2 | + |
| 3 | +## 前言 |
| 4 | + |
| 5 | + 笔者最近开始投简历,出于应对之后快速的面试流程需求,这里准备的是将常见的C++八股文进行总结,从而方便自己进行学习,检查和评估。 |
| 6 | + |
| 7 | +## 什么是虚函数 |
| 8 | + |
| 9 | + 虚函数,本质上还是函数,为什么是虚的呢?虚函数本质上是由 `virtual` 修饰的成员函数,但“虚”的真正含义指的是:它的调用行为并非在编译阶段决定,而是在运行阶段通过动态绑定机制决定。举一个例子感受一下: |
| 10 | + |
| 11 | +```cpp |
| 12 | +class BaseClass { |
| 13 | +public: |
| 14 | + virtual void aVirtualFunction() = 0; // 这是一个最极端的纯虚函数 |
| 15 | +}; |
| 16 | + |
| 17 | +class DerivedClass : public BaseClass { |
| 18 | +public: |
| 19 | + void aVirtualFunction() override; // 这是子类的一个实现 |
| 20 | +}; |
| 21 | +``` |
| 22 | +
|
| 23 | + 如果你熟悉,你就会说:很简单,当我们写上代码: |
| 24 | +
|
| 25 | +```cpp |
| 26 | +BaseClass* pSon = new DerivedClass(); |
| 27 | +pSon->aVirtualFunction(); |
| 28 | +``` |
| 29 | + |
| 30 | + 的时候,我们就会高兴的发现,尽管我们在书写的时候写的是BaseClass*,但是实际上,由于我们的赋值对象是BaseClass的派生类DerivedClass,这个时候,aVirtualFunction被瞧瞧的替换成了子类的实现而不是父类的实现。**这就是一种运行时的多态!**大类中的子类行为可以被共同表达为一个抽象的行为,而子类则各自为政,具象化这个抽象的父类行为(或者是覆盖父类的行为,这就是类的覆盖了,我们后面讨论类的时候慢慢聊)。 |
| 31 | + |
| 32 | +## 虚函数的实现本质 |
| 33 | + |
| 34 | + 我们现在聊一聊虚函数的运作本质是如何支持的。很简单,写过嵌入式C比较大型项目的朋友都知道,我们经常搞函数回调,也就是说,在运行的时候动态的跳转道给定的地址执行代码。在C++中,我们可以联想到——上述的这些本质上可以看作是类中的一个动态的存储着函数指针的成员变量,发生继承的时候,我们就把子类的指针赋值给我们的对象上,这样,发生调用的时候,我们就会直接调用这个函数指针指向的函数。很好!实际上大致的确如此,我们把这些潜在的虚函数排列成一张表格。也就是经典的虚函数表。虚函数表是用一个虚函数表指针指向的。所以说,**指向虚函数表成员的大小就是一个指针的大小**,这是毋庸置疑的。**这就是你调试一个带有虚函数的类的时候,你看到调试栏中的`_vptr`或者`_vtable`**对象了。 |
| 35 | + |
| 36 | +> 免责声明: |
| 37 | +> |
| 38 | +> - `_vptr` 是编译器生成的,名称和布局是实现相关的(如 MSVC 和 GCC 不一样),也就是说,不同的编译器对此的实现不一致,所以别乱搞Hack把代码的可移植性搞丢了 |
| 39 | +> - 不是标准定义的,因此依赖它做 portable 编程是不可取的,但了解它对理解原理很重要。 |
| 40 | + |
| 41 | +## 一些其他重要的事情 |
| 42 | + |
| 43 | +- 派生类中重写时,**可以省略 `virtual`**,虚性自动继承。 |
| 44 | + |
| 45 | +- 可以用 `override` 强制编译器检查函数签名是否匹配,避免拼写或参数错误 |
| 46 | + |
| 47 | +- **纯虚函数**(`=0`)表示“子类必须重写”,容许定义抽象类,也就是无法实例化的接口 。这个小trick可以用在库设计中,强迫客户程序员重写父类的代码 |
| 48 | + |
| 49 | +- **虚析构函数**用来确保通过基类指针删除子类对象时,析构链能正确触发。所以,任何带有虚函数的类,都最好将自己的析构函数写上大大的`virtual`,否则会翻车。 |
| 50 | + |
| 51 | + ```cpp |
| 52 | + struct A { |
| 53 | + ~A() { std::cout << "A析构\n"; } |
| 54 | + }; |
| 55 | + struct B : A { |
| 56 | + ~B() { std::cout << "B析构\n"; } |
| 57 | + }; |
| 58 | + |
| 59 | + A* p = new B(); |
| 60 | + delete p; // 只会调用A的析构! |
| 61 | + ``` |
| 62 | +
|
| 63 | + 这个时候你需要做的是: |
| 64 | +
|
| 65 | + ```cpp |
| 66 | + struct A { |
| 67 | + virtual ~A() { std::cout << "A析构\n"; } |
| 68 | + }; |
| 69 | + ``` |
| 70 | + |
| 71 | +- 静态函数不能是虚函数:它不属于实例,没 `this` 指针,无法参与动态绑定 |
| 72 | + |
| 73 | +# 到这里就结束?No No No |
| 74 | + |
| 75 | + 那可太没意思了兄弟们,大伙都知道我这个人的性子,写着点是没有啥值得说的,否则就成了博客灌水了。我们还有很多其他的话题。 |
| 76 | + |
| 77 | +## 多重继承下的虚函数表结构(主次 vtable) |
| 78 | + |
| 79 | + 我们知道,C++是一个允许多重继承的语言,请看下图: |
| 80 | + |
| 81 | +```c++ |
| 82 | +class HolyShitComplexClass : public BaseA, // is BaseA |
| 83 | + public BaseB, // is BaseB |
| 84 | + public BaseC, // is Also BaseC |
| 85 | +{ |
| 86 | + ... |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | + 这个所谓的HolyShitComplexClass是三个类的子集,我们这个时候会问,欸!这么多的父类,我们的虚函数表怎么排列呢?答案是这样的,按照我们继承的顺序,依次排放我们的虚函数表指针,这也就是说,在多重继承中,每个基类子对象通常都有独立的 `vptr` 和对应的 vtable(主表与次表),使得各继承路径可独立动态绑定 |
| 91 | + |
| 92 | +``` |
| 93 | +[vptr_A][A_fields][vptr_B][B_fields][vptr_C][C_fields][Derived_fields] |
| 94 | +``` |
| 95 | + |
| 96 | + 你看,这样排列的,我要是重写了A中的一个虚函数的行为,咱们就改A函数表中对应的函数指针指向,其他的如法炮制! |
| 97 | + |
| 98 | +## **虚函数与非虚函数混合时的内存布局** |
| 99 | + |
| 100 | + 更多的时候,我们往往是类中同时含有虚函数和非虚数据成员,笔者的CCIMXDesktop项目中就有大量的这样的例子: |
| 101 | + |
| 102 | +```cpp |
| 103 | +#ifndef APPCARDWIDGET_H |
| 104 | +#define APPCARDWIDGET_H |
| 105 | + |
| 106 | +#include <QWidget> |
| 107 | + |
| 108 | +class DesktopToast; |
| 109 | +class QLabel; |
| 110 | +namespace Ui { |
| 111 | +class AppCardWidget; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief AppCardWidget is a lightweight widget used to post messages to a DesktopToast. |
| 116 | + * |
| 117 | + * This is an abstract base class representing an application card UI component. |
| 118 | + * It is responsible for handling pre-launch work and posting messages via toast notifications. |
| 119 | + */ |
| 120 | +class AppCardWidget : public QWidget { |
| 121 | + Q_OBJECT |
| 122 | + |
| 123 | +public: |
| 124 | + Q_DISABLE_COPY(AppCardWidget); |
| 125 | + AppCardWidget() = delete; |
| 126 | + |
| 127 | + /** |
| 128 | + * @brief Constructs an AppCardWidget. |
| 129 | + * @param toast Pointer to the DesktopToast object used to show messages. |
| 130 | + * @param parent Optional parent widget. |
| 131 | + */ |
| 132 | + explicit AppCardWidget(DesktopToast* toast, QWidget* parent = nullptr); |
| 133 | + ~AppCardWidget(); |
| 134 | + |
| 135 | + /** |
| 136 | + * @brief Set the current icon for the app card. |
| 137 | + * |
| 138 | + * This function allows derived classes to customize the app card icon |
| 139 | + * by providing a QPixmap. |
| 140 | + * |
| 141 | + * @param icons The pixmap to be used as the icon. |
| 142 | + */ |
| 143 | + virtual void setCurrentIcon(const QPixmap& icons); |
| 144 | + |
| 145 | + /** |
| 146 | + * @brief Abstract method to invoke pre-launch operations. |
| 147 | + * |
| 148 | + * Derived classes should implement this to perform necessary |
| 149 | + * preparations before the system starts or the app card becomes active. |
| 150 | + */ |
| 151 | + virtual void invoke_preLaunch_work() = 0; |
| 152 | + /** |
| 153 | + * @brief operate_comment_label |
| 154 | + */ |
| 155 | + virtual void operate_comment_label() = 0; |
| 156 | + |
| 157 | + /** |
| 158 | + * @brief invoke_textlabel_stylefresh |
| 159 | + */ |
| 160 | + void invoke_textlabel_stylefresh(); |
| 161 | + |
| 162 | +protected: |
| 163 | + /** |
| 164 | + * @brief Abstract method to post messages to the bound DesktopToast. |
| 165 | + * |
| 166 | + * Derived classes implement this to send notifications or status updates |
| 167 | + * through the toast system. |
| 168 | + */ |
| 169 | + virtual void postAppCardWidget() = 0; |
| 170 | + |
| 171 | + /** |
| 172 | + * @brief setHelperFunction: plainly set the text for shown |
| 173 | + * @param what |
| 174 | + */ |
| 175 | + virtual void setHelperFunction(const QString& what); |
| 176 | + |
| 177 | + virtual void setupSelfTextLabelStyle(QLabel* selfTextLabel) = 0; |
| 178 | + |
| 179 | + DesktopToast* binding_toast; ///< Pointer to the toast widget used for posting messages. |
| 180 | + Ui::AppCardWidget* ui; ///< UI object generated from the Qt Designer form. |
| 181 | + |
| 182 | +public: |
| 183 | + /** |
| 184 | + * @brief Event filter to handle user interaction events. |
| 185 | + * @param watched The QObject being watched. |
| 186 | + * @param event The event being filtered. |
| 187 | + * @return true if the event was handled, otherwise false. |
| 188 | + */ |
| 189 | + bool eventFilter(QObject* watched, QEvent* event) override; |
| 190 | +}; |
| 191 | +#endif // APPCARDWIDGET_H |
| 192 | +``` |
| 193 | + |
| 194 | + 你看,这个类中,我们就混合了虚函数和非虚函数,那么问题来了,我们的编译器有没有比较具体的排列呢?有的。 |
| 195 | + |
| 196 | +- 通常 `vptr` 放在对象首部(首成员); |
| 197 | +- 紧接其后是非虚成员,保持自然对齐; |
| 198 | +- 所有虚函数都在单一 vtable 中按声明顺序排列 。 |
| 199 | + |
| 200 | +``` |
| 201 | ++---------------------+--------------------------------------------+ |
| 202 | +| 内存区域 | 内容描述 | |
| 203 | ++=====================+============================================+ |
| 204 | +| 虚表指针 | 指向 AppCardWidget 的虚函数表 | |
| 205 | ++---------------------+--------------------------------------------+ |
| 206 | +| | | |
| 207 | +| 成员变量 | DesktopToast* binding_toast | |
| 208 | +| | (绑定的 toast 对象指针) | |
| 209 | +| +--------------------------------------------+ |
| 210 | +| | Ui::AppCardWidget* ui | |
| 211 | +| | (UI 组件指针) | |
| 212 | ++---------------------+--------------------------------------------+ |
| 213 | +| 继承部分 | QWidget 基类的所有成员数据 | |
| 214 | ++---------------------+--------------------------------------------+ |
| 215 | +| Qt 特性 | Q_OBJECT 宏添加的元对象系统数据 | |
| 216 | +| +--------------------------------------------+ |
| 217 | +| | Q_DISABLE_COPY 宏禁用拷贝功能 | |
| 218 | ++---------------------+--------------------------------------------+ |
| 219 | +
|
| 220 | +虚表指针指向的是-> |
| 221 | ++-----+---------------------------------------------+-----------+ |
| 222 | +| 偏移 | 函数签名 | 类型 | |
| 223 | ++=====+=============================================+===========+ |
| 224 | +| 0 | ~AppCardWidget() | 析构函数 | |
| 225 | ++-----+---------------------------------------------+-----------+ |
| 226 | +| 1 | setCurrentIcon(const QPixmap&) | 虚函数 | |
| 227 | ++-----+---------------------------------------------+-----------+ |
| 228 | +| 2 | invoke_preLaunch_work() | 纯虚函数 | |
| 229 | ++-----+---------------------------------------------+-----------+ |
| 230 | +| 3 | operate_comment_label() | 纯虚函数 | |
| 231 | ++-----+---------------------------------------------+-----------+ |
| 232 | +| 4 | postAppCardWidget() | 纯虚函数 | |
| 233 | ++-----+---------------------------------------------+-----------+ |
| 234 | +| 5 | setHelperFunction(const QString&) | 虚函数 | |
| 235 | ++-----+---------------------------------------------+-----------+ |
| 236 | +| 6 | setupSelfTextLabelStyle(QLabel*) | 纯虚函数 | |
| 237 | ++-----+---------------------------------------------+-----------+ |
| 238 | +| 7 | eventFilter(QObject*, QEvent*) | 虚函数 | |
| 239 | ++-----+---------------------------------------------+-----------+ |
| 240 | +``` |
| 241 | + |
| 242 | +## CRTP 静态多态 |
| 243 | + |
| 244 | + 这个就谈不上虚函数的内容了,但是放在这里原因很简单,我们谈论到运行时多态的时候,必然要跟CRTP 静态多态做做对比。 |
| 245 | + |
| 246 | + 在很多时候,我们可能实际上,不太需要的是运行时多态,什么意思? |
| 247 | + |
| 248 | +```cpp |
| 249 | +BaseClass* pSon = new DerivedClass(); |
| 250 | +pSon->aVirtualFunction(); |
| 251 | +``` |
| 252 | + |
| 253 | + 这个代码显然我们并不需要运行时多态,理由非常简单,**因为我们知道pSon在这个逻辑流中指向的是一个具体的子类DerivedClass**,而不是其他奇奇怪怪的东西。基于这个理念,我们发现一些场景中完全不需要所谓的运行时多态,我们在写代码的时候就预料到这里一定不会出现运行才能裁决的事情,啥叫运行时裁决呢? |
| 254 | + |
| 255 | +```cpp |
| 256 | +void DesktopMainWindow::invoke_appcards_init() { |
| 257 | + /* sequencely invoke the work */ |
| 258 | + showToast("AppCards Are Initing..."); |
| 259 | + // each_app_cards是一个父类,这个父类表达这个对象是一个AppCards,但是具体是啥,如何提前派发初始化的工作,由子类自己的invoke_preLaunch_work裁决。 |
| 260 | + for (const auto& each_app_cards : std::as_const(this->app_cards)) { |
| 261 | + each_app_cards->invoke_preLaunch_work(); |
| 262 | + } |
| 263 | + showToast("AppCards Init Finished!"); |
| 264 | +} |
| 265 | +``` |
| 266 | + |
| 267 | + 这个卡片可能实从用户提供的插件动态库,将来的我创建的,但是现在我完全不知道他们的具体的行为,但是我可以保证他们肯定至少是AppCards,这个时候就要到运行的时候检索类对象的元信息找出她到底是谁,然后调用具体的类对invoke_preLaunch_work的实现 |
| 268 | + |
| 269 | + 所以,CRTP是啥呢?答案是:Curiously Recurring Template Pattern,奇异递归模板模式。这个玩意的基本起手框架是这样的。。。 |
| 270 | + |
| 271 | +```c++ |
| 272 | +template <typename Derived> |
| 273 | +class Base { |
| 274 | +public: |
| 275 | + void interface() { |
| 276 | + // 调用派生类实现的方法 |
| 277 | + static_cast<Derived*>(this)->implementation(); |
| 278 | + } |
| 279 | + |
| 280 | + // 可提供默认实现(可选) |
| 281 | + void implementation() { |
| 282 | + std::cout << "Base implementation\n"; |
| 283 | + } |
| 284 | +}; |
| 285 | + |
| 286 | +class Derived : public Base<Derived> { |
| 287 | +public: |
| 288 | + void implementation() { |
| 289 | + std::cout << "Derived implementation\n"; |
| 290 | + } |
| 291 | +}; |
| 292 | +``` |
| 293 | +
|
| 294 | + 这很有意思,我们实际上利用模板,在编译的时候就转发给了我们指定的编译器确定的Derived类。这就是CRTP。我们可以看到,这里在语法上,Base和Derived可以说是毫无关系,我们在代码编写的时候才会耦合到一起。 |
| 295 | +
|
| 296 | +- 这个可是和传统虚函数的动态多态不同,CRTP 在 **编译期**就完成了多态行为的分发(通过 `static_cast` 强转为派生类),没有虚函数表、没有间接跳转; |
| 297 | +- 不会因为虚函数造成 cache miss 或影响内联优化; |
| 298 | +- 可以实现“接口继承”或“行为注入”。 |
| 299 | +
|
| 300 | + 所以CRTP在现代C++中还是有不少的身影的(我没用到,因为我评估我的项目是否可以有潜在的提升的时候发现真用不上)。 |
| 301 | +
|
| 302 | + 感谢GPT,他给了我一个总结表格: |
| 303 | +
|
| 304 | +| 特性 | CRTP(静态多态) | 虚函数(动态多态) | |
| 305 | +| --------------- | ------------------------ | ------------------ | |
| 306 | +| 多态分发时机 | 编译期 | 运行时 | |
| 307 | +| 是否使用 vtable | 否 | 是 | |
| 308 | +| 性能开销 | 无额外开销,可内联优化 | 存在虚函数调用开销 | |
| 309 | +| 类型灵活性 | 编译期固定,类型必须已知 | 支持运行时多态 | |
| 310 | +| 扩展性 | 模板层级难以抽象出接口 | 更适合框架级接口 | |
| 311 | +
|
| 312 | +### 🧱 CRTP 限制和注意事项 |
| 313 | +
|
| 314 | + CRTP是存在问题的,我们一个一个了解: |
| 315 | +
|
| 316 | +- **编译复杂度增加**:模板膨胀、编译错误难排查,我想大家都被该死的模板报错折磨过(好像新clang对模板的报错稍微友善了点?但是排查模板的报错属于是非常的不友好了) |
| 317 | +- **不可跨 DLL 边界使用**:CRTP 依赖编译期展开,这就是因为它本身就是一个依赖模板的静态多态技术,属于是代价了。 |
| 318 | +- **无法通过指针存储基类对象**:除非用 `Base<Derived>*` 这样的具体类型; |
| 319 | +- **类型绑定固定**:CRTP 的“接口”只能绑定一个特定派生类,缺乏动态扩展能力; |
0 commit comments