c++ - 将 int 地址转换为 char 指针的正确方法

标签 c++ pointers casting

如果我需要从 ifstream 读取 int

int myInt = 0;
fileStream.read(reinterpret_cast<char*>(&myInt), sizeof(int));

正在使用 reinterpret_cast<char*>实现该目标的正确方法?

最佳答案

is using reinterpret_cast correct way to accomplish that?

是的。更喜欢 C++ 风格的转换,而不是 C 风格的转换。

正如评论中所建议的,使用 read method function 的更好方法是:

int myInt = 0;
fileStream.read(reinterpret_cast<char*>(&myInt), sizeof(myInt));

关于c++ - 将 int 地址转换为 char 指针的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8576415/

相关文章:

c++ - Qt C++、Eclipse(在 Win 7 上)- 项目构建失败,出现 "unresolved reference to _Unwind_Resume"错误

c++ - 在光线追踪器中实现软阴影

c++ - 在没有 const_cast 的情况下将 const void* 转换为其他指针时出错

c - 只要它们包含 "next"字段,是否可以在 C 中为不同的列表结构编写通用遍历函数?

c - 将 printf 与指向 float 的指针一起使用会产生错误

c# - 如何使用不同类型进行类型转换

c++ - 如何在 C++ 中将 long 转换为字符串?

c++ - 是否可以用 wincrypt 做一个 HMAC?

c++ - c++中删除变量的详细信息

c++ - vector 作为函数的参数 (int**)