在C++中,类型转换主要有以下几种方法:
静态转换:使用static_cast关键字进行类型转换,适用于基本数据类型之间的转换,以及具有继承关系的类之间的转换。int a = 10;double b = static_cast<double>(a);动态转换:使用dynamic_cast关键字进行类型转换,用于类之间的多态类型转换,只能用于具有虚函数的类。class Base {public: virtual void func() {}};class Derived : public Base {};Base* basePtr = new Derived;Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);重新解释转换:使用reinterpret_cast关键字进行类型转换,不进行类型检查,直接将一个指针或引用转换为另一种类型。int* ptr = new int(10);char* charPtr = reinterpret_cast<char*>(ptr);const转换:使用const_cast关键字进行类型转换,用于去除const属性,只能用于指针或引用。const int a = 10;int& b = const_cast<int&>(a);旧式的C风格转换:使用C语言的风格进行类型转换,不建议使用,因为不安全。int a = 10;double b = (double)a; 



