reinterpret_cast?坏?好? [英] reinterpret_cast ? bad? good?

查看:106
本文介绍了reinterpret_cast?坏?好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将整数表保存到文件中。

在文件中将单元格写成固定大小的一种方法是使用

reinterpret_cast。

这是一个糟糕的选择,不错的选择?我记得有一次我发布了一个程序

使用它,有些人建议我使用它可能会出错。

有没有其他方法可以将单元格写成固定大小在该文件中?

(知道电池将只包含整数)。
$ b $这里b为我计划如何做到这一点(没有所有的细节多少关怀),


int cell = 20;

outFile.write(reinterpret_cast< const char *>(& cell),sizeof(cell));


THX的任何反馈


-

。从天气预报员语录:

罗伯特· Spritz:你知道做的事情更难,而且正确的事情要做b $ b,这通常都是一回事吗? "易"没有进入成年人生活......为了得到任何有价值的东西,你必须牺牲。

解决方案



Someonekicked写道:

我想将整数表保存到文件中。
在文件中将单元格写成固定大小的一种方法是使用
reinterpret_cast。
这是一个糟糕的选择,不错的选择?我记得曾经发布过一个使用它的程序,有些人建议我使用它可能会出错。
还有其他方法可以将文件写入固定大小的文件吗?
(知道单元格只包含整数)。

这是我打算如何做的(没有太多关心所有细节),

int cell = 20;
outFile.write(reinterpret_cast< const char *>(& cell),sizeof(cell));




reinterpret_cast很糟糕。但是,它有时是必要的吗?好吧,

你刚刚展示了一个区域。实际上没有任何其他输出到二进制文件的方式比你上面的方式。你有'b $ b'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' />
chunks ... chars。这个行为当然会导致可移植性,但是将二进制数据写入文件的行为本身并不是可移植的......你不能把这个文件带到b $ b任何一台电脑,并期望它没有考虑

小心翼翼地系统之间的转换工作。


的reinterpret_cast表示不可移植的代码使用它的任何时间。作为

这样的,你使用它的时候,你要问自己,如果你是什么

这样做,其实是正确的。引发更多标志的唯一演员阵容是

const_cast。


您可以通过转换为网络端来使您的IO更具可移植性

在写之前(并在阅读后主持enadian) - 与通过套接字发送时
相同的方式。


>>这是我打算如何做到的(没有太多关心所有细节),


int cell = 20;
outFile.write (reinterpret_cast的<常量字符*>(&安培;细胞),的sizeof(小区));


的reinterpret_cast是坏的。但是,它有时是必要的吗?好吧,
你刚刚展示了一个区域。输出到二进制文件的方式实际上没有任何其他方式。你有把你的数据''翻译'到字节,所以在某个地方,你必须重新解释转换为按字母顺序地将数据转换成字节分隔的块...字符。这个行为当然会导致可移植性,但是将二进制数据写入文件的行为本身并不是可移植的......你不能将该文件带到任何计算机并期望它能够在没有它的情况下运行非常谨慎地在系统之间进行翻译。



同意。


也就是说,整数被重新解释为a的事实char *

更好地隐藏了用户,因为它确实是一种实现技术。


一种方法是使用重载函数:


void write_data(const File_stream& outFile,int target)

{

char * p = reinterpret_cast< char *>(* target );

outFile.write(p,sizeof(int));

}


这样用户可以简单地使用它如下:


int i = 42;

File_stream f(" myfile");

write_data(f ,i);


如果不知何故你有比reinterpret_cast更好的选择回报

只是你需要改变的地方,而不是检查整个

项目。


问候,
Ben



I want to save tables of integers to files.
One way to write the cells as fixed size in the file, is to use
reinterpret_cast.
Is that a bad choice, good choice? I remember once before I posted a program
using it, and some suggested that I might get errors using it.
are there any other ways to write the cells as fixed size in the file?
(knowing the cells will only contain integers).
here is how I plan to do it (without much caring about all the details),

int cell = 20;
outFile.write(reinterpret_cast<const char *> (&cell),sizeof(cell));

thx for any feedback.

--
Quotes from The Weather Man:
Robert Spritz: Do you know that the harder thing to do, and the right thing
to do, are usually the same thing? "Easy" doesn''t enter into grown-up
life... to get anything of value, you have to sacrifice.

解决方案


Someonekicked wrote:

I want to save tables of integers to files.
One way to write the cells as fixed size in the file, is to use
reinterpret_cast.
Is that a bad choice, good choice? I remember once before I posted a program
using it, and some suggested that I might get errors using it.
are there any other ways to write the cells as fixed size in the file?
(knowing the cells will only contain integers).
here is how I plan to do it (without much caring about all the details),

int cell = 20;
outFile.write(reinterpret_cast<const char *> (&cell),sizeof(cell));



reinterpret_cast is bad. However, is it sometimes necessary? Well,
you have just shown one area where it is. There isn''t really any other
way to output to a binary file than what you are doing above. You have
to ''translate'' you data to bytes so somewhere down the line you have to
do a reinterpret cast to bitwise cast your data into byte delimited
chunks...chars. This act of course kills portability, but the very act
of writing binary data to a file is itself not portable...you can''t
take that file to any computer and expect it to work without taking
great care to translate between systems.

reinterpret_cast indicates non-portable code any time it is used. As
such, any time you do use it you need to ask yourself if what you are
doing is in fact correct. The only cast that raises more flags is a
const_cast.


you can make your IO more portable by converting to network endian
before writing (and to host enadian after reading) - the same way you
would do when sending through a socket.


>> here is how I plan to do it (without much caring about all the details),


int cell = 20;
outFile.write(reinterpret_cast<const char *> (&cell),sizeof(cell));



reinterpret_cast is bad. However, is it sometimes necessary? Well,
you have just shown one area where it is. There isn''t really any other
way to output to a binary file than what you are doing above. You have
to ''translate'' you data to bytes so somewhere down the line you have to
do a reinterpret cast to bitwise cast your data into byte delimited
chunks...chars. This act of course kills portability, but the very act
of writing binary data to a file is itself not portable...you can''t
take that file to any computer and expect it to work without taking
great care to translate between systems.


Agree.

That said, the fact that the integer is reinterpret_casted to a char*
is better hidden from the user as it is indeed an implementation technique.

One way to do this is to use overloaded functions:

void write_data(const File_stream& outFile, int target)
{
char* p = reinterpret_cast<char*>(*target);
outFile.write(p, sizeof(int));
}

This way the user can simply use it as the following:

int i = 42;
File_stream f("myfile");
write_data(f, i);

If somehow you have better alternative than the reinterpret_cast there
is only one place you need to change, instead of inspecting the whole
project.

Regards,
Ben


这篇关于reinterpret_cast?坏?好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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