正确的方式将int地址转换为char指针 [英] Correct way to cast address of int to char pointer

查看:1461
本文介绍了正确的方式将int地址转换为char指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要从ifstream读取int

If I need to read int from ifstream

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

正在使用 reinterpret_cast< char *> 正确的方式来完成这一点?

is using reinterpret_cast<char*> correct way to accomplish that?

推荐答案


正在使用reinterpret_cast正确的方法来完成? >

is using reinterpret_cast correct way to accomplish that?

是的。首选c ++样式转换,而不是c样式转换。

Yes. Prefer c++ style casts, instead of c style casts.

如注释中所建议的,使用读取方法函数是:

As suggested in comments, a better way to use the read method function is :

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

这篇关于正确的方式将int地址转换为char指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆