瀏覽代碼

feat: 优化虚函数的获取写法

NiceTry12138 5 月之前
父節點
當前提交
a922cdbd6b
共有 1 個文件被更改,包括 21 次插入0 次删除
  1. 21 0
      cpp/现代C++/现代C++.md

+ 21 - 0
cpp/现代C++/现代C++.md

@@ -431,6 +431,27 @@ int main() {
 }
 ```
 
+优化一下虚函数执行的写法,使用 `std::uintptr_t` 来表示指针大小,使用 `vtable[0]` 这种数组方式来更加安全的读取函数指针
+
+```cpp
+// 读 vptr(对象开头的第一个指针)
+std::uintptr_t vptr = *reinterpret_cast<std::uintptr_t*>(b);
+
+// 把 vptr 当作指向“函数地址数组”的指针
+auto vtable = reinterpret_cast<std::uintptr_t const*>(vptr);
+
+// 取前三个虚函数地址并调用
+Fn f0 = reinterpret_cast<Fn>(vtable[0]); // A::fun
+Fn f1 = reinterpret_cast<Fn>(vtable[1]); // B::fun1 (override)
+Fn f2 = reinterpret_cast<Fn>(vtable[2]); // B::fun2
+
+f0(b);
+f1(b);
+f2(b);
+
+delete b;
+```
+
 ### 空类的内存模型
 
 ```cpp