C++ 类型转换运算符有以下几种:
int num = 42;
double d = static_cast<double>(num);
class Base { virtual ~Base() {} };
class Derived : public Base {};
Base* b = new Derived();
Derived* d = dynamic_cast<Derived*>(b); // 安全的转换
const int num = 42;
int* p = const_cast<int*>(&num); // 修改 num 的值,不推荐
int num = 42;
char* c = reinterpret_cast<char*>(&num); // 将 int 转换为 char*
使用类型转换运算符时,需要注意以下几点:
std::stod
、std::to_string
等。亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C++类型转换运算符怎样使用