| 整型提升 | 较小的整数类型提升为较大的整数类型,例如char提升为int。 | char c = 5; int i = c; |
| 算术转换 | 不同数值类型参与运算时的类型转换,例如float和int混合运算。 | float f = 3.14; int i = 2; double d = f * i; |
| 指针与整数 | 整数可以转换为指针,指针可以转换为整数。 | int* p = reinterpret_cast<int*>(0x1000); |
| 指针与布尔 | 指针可以隐式转换为布尔值。 | if (nullptr) { /* 空指针为false */ } |
| 派生类到基类 | 派生类指针或引用可以隐式转换为基类指针或引用。 | Derived d; Base* b = &d; |
| 类对象到基类 | 类对象可以隐式转换为基类对象。 | Base base = Derived(); |
| 枚举到整数 | 枚举类型可以隐式转换为其底层整数类型。 | enum Color { Red, Green, Blue }; int i = Red; |
| C风格字符串到std::string | C风格字符串可以隐式转换为std::string。 | std::string s = "Hello"; |
| std::string到const char* | std::string可以隐式转换为const char*。 | std::string s = "Hello"; const char* cstr = s.c_str(); |
| 数组到std::vector | 数组可以隐式转换为std::vector。 | int arr[] = {1, 2, 3}; std::vector<int> vec(arr, arr + 3); |
| 用户定义的转换 | 通过转换构造函数或类型转换运算符实现自定义的隐式类型转换。 | class MyClass {public: operator int() const { return 42; }}; MyClass mc; int val = mc; |