英文:
When reading from a binary stream, what does it mean to cast the address of a double variable to a char*?
问题
以下是翻译好的部分:
我从一个在线的C++课程中获得了以下代码:
class tempStat
{
public:
// 数据成员。
double minimum, maximum;
// 构造函数。
tempStat(double min = 0.0, double max = 0.0)
: minimum(min), maximum(max)
{}
// 辅助方法,将tempStat对象以二进制格式写入文件。
void write(std::ostream & os)
{
os.write((char*)&minimum, sizeof(double));
os.write((char*)&maximum, sizeof(double));
}
// 辅助方法,从二进制文件中读取tempStat对象。
void read(std::istream & is)
{
is.read((char*)&minimum, sizeof(double));
is.read((char*)&maximum, sizeof(double));
}
}
这个类的使用方式如下:
tempStat ts;
ts.read(ifile);
我的问题是,我们如何将一个内存地址(&minimum
)强制转换为char
指针?
其次,如果minimum
是一个int
,我们可能会写相同的代码(至少在(char*)&minimum
部分),那么&someIntVariable
怎么能被强制转换为char*
呢?
英文:
I have the following code from an online C++ course:
class tempStat
{
public:
// Data members.
double minimum, maximum;
// Constructor.
tempStat(double min = 0.0, double max = 0.0)
: minimum(min), maximum(max)
{}
// Helper method, to write a tempStat object to a file in binary format.
void write(std::ostream & os)
{
os.write((char*)&minimum, sizeof(double));
os.write((char*)&maximum, sizeof(double));
}
// Helper method, to read a tempStat object from a file in binary format.
void read(std::istream & is)
{
is.read((char*)&minimum, sizeof(double));
is.read((char*)&maximum, sizeof(double));
}
}
This class is used like so:
tempStat ts;
ts.read(ifile);
My question is, how can we cast a memory address (&minimum
) as a pointer to a char
?
Secondly, if minimum
had been an int
, presumably we'd have written the same code (at least, as far as (char*)&minimum
), so how does it work that &someIntVariable
can also be cast as a char*
?
答案1
得分: 7
流中的信息以字节序列(char
)的形式呈现。
您想要将该信息读入变量minimum
和maximum
中,它们都是double
类型的。
您假设您正在读取的字节构成了系统上double
的有效字节表示。
您将double
别名为字节序列(这是标准中允许的一种特殊规则;通常情况下,您不能假装某物是其他东西),然后将结果的char*
传递给流的“read”函数。
流的“read”函数认为它被要求将数据放入char
数组中,因为char*
看起来像这样,而且该函数的设计也是如此。
因此,字节序列被“read”函数复制到您的double
中。
对于write
函数也是一样的。我们假装向流中提供了一个字节序列(char
数组)以进行写入,我们被允许这样做,因为从本质上讲,double
就是这样的。
其次,如果minimum
是一个int
,我们可能会写相同的代码(至少,在(char*)&minimum
这一点上是如此),那么&someIntVariable
如何也能被强制转换为char*
呢?
因为int
也是由一系列字节表示的。
每个对象都是。
英文:
The information in the stream is presented in the form of a sequence of bytes (char
s).
You want to read that information into the variables minimum
and maximum
, which are double
s.
You assume that the bytes you're reading constitute a valid byte representation of a double
on your system.
You "alias" the double
s as a sequence of bytes (this is permitted as a special rule in the standard; generally you can't just pretend something is something else), then give the resulting char*
to the stream's "read" function.
The stream's "read" function thinks it's being asked to put the data into an array of char
, because that's what a char*
looks like, and because that's what the function is designed to do.
Thus, the sequence of bytes is copied by the "read" function, into your double
s.
It's the same with the write
function. We're pretending to the stream that we're giving it a sequence of bytes (a char
array) to write, and we're allowed to do that because that's fundamentally what a double
is, underneath it all.
> Secondly, if minimum had been an int, presumably we'd have written the same code (at least, as far as (char*)&minimum), so how does it work that &someIntVariable can also be cast as a char*?
Because an int
is represented by a sequence of bytes, too.
Every object is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论