类型转换时出错.混乱

Error when type casting. Confusion

本文关键字:混乱 出错 类型转换      更新时间:2023-10-16
double d = 43.56;
int m = d;
cout<<(char *)&m<<endl; //works fine, it prints: + = 43
cout<<(char *)m<<endl; //this doesn't work, char can't be made into a  pointer
cout<<reinterpret_cast<char *>(&m)<<endl; //works fine, prints: +
cout<<static_cast<char *>(&m)<<endl; //Does not work

我的问题是,为什么最后一行代码不起作用?

编译器错误消息:invalid static_cast from int* to type char*

难道static_cast不能转换这个吗?

以下是static_cast的完整列表。和不能将指向一个类的指针强制转换为指向另一个不相关类的指针。因为这不是标准的c++行为。
然而,你仍然可以使用c风格的cast, reinterpret_cast,甚至"cast"与union