fix Casting Operators

https://github.com/huihut/interview/issues/113
This commit is contained in:
huihut
2025-07-25 20:45:01 +08:00
parent b7408d80ef
commit f7af99f12c

View File

@@ -1102,7 +1102,7 @@ unique_ptr 是 C++11 才开始提供的类型,是一种在异常时可以帮
| 转换类型 | 安全性 | 示例 | | 转换类型 | 安全性 | 示例 |
|--------------------------|--------------|--------------------------| |--------------------------|--------------|--------------------------|
| 数值类型转 | ✅ 安全 | `float f=3.14; int i=static_cast<int>(f);` | | 数值类型转 | ✅ 安全 | `float f=3.14; int i=static_cast<int>(f);` |
| 类向上转换 | ✅ 安全 | `Derived* d; Base* b=static_cast<Base*>(d);` | | 类向上转换 | ✅ 安全 | `Derived* d; Base* b=static_cast<Base*>(d);` |
| 类向下转换 | ⚠️ 不安全 | `Base* b=new Base; Derived* d=static_cast<Derived*>(b);` | | 类向下转换 | ⚠️ 不安全 | `Base* b=new Base; Derived* d=static_cast<Derived*>(b);` |
| 类不变转换 | ✅ 安全 | `MyClass* p; MyClass* same=static_cast<MyClass*>(p);` | | 类不变转换 | ✅ 安全 | `MyClass* p; MyClass* same=static_cast<MyClass*>(p);` |