结构布局 [英] Structure layout
问题描述
您可能知道,std :: fwrite仅适用于POD类(或者至少在
中,我建议仅在POD类上使用它...)。我有这个POD类,我想要添加方法,因此我必须替换我需要调用std :: fwrite的代码部分。
例如,我有
struct foo {
int a;
char b ;
};
// ...
foo f;
FILE * file ;
std :: fwrite(& f,1,sizeof(f),file);
// ...
如果我向foo添加一个方法,我该如何更改代码片段?
-
我只是海市蜃楼。
As you might know, std::fwrite only works on POD classes (or in the
very least, I am advised to use this only on POD classes...). I have
this POD class that I want to add methods, and thus have to replace
parts of my code where I have to call std::fwrite.
For example, I have
struct foo {
int a;
char b;
};
// ...
foo f;
FILE* file;
std::fwrite (&f, 1, sizeof(f), file);
// ...
If I add a method to foo, how would I change the code snippet?
--
I am only a mirage.
推荐答案
kelvSYC写道:
kelvSYC wrote:
正如您所知,std :: fwrite仅适用于POD类(或
至少,我建议只在POD课上使用它...)。我有这个POD类,我想添加方法,因此必须替换我必须调用std :: fwrite的部分代码。
例如,我有
struct foo {
int a;
char b;
};
// ...
foo f;
FILE *文件;
std :: fwrite(& f,1,sizeof(f),file);
// ...
>如果我向foo添加方法,我将如何更改代码片段?
As you might know, std::fwrite only works on POD classes (or in the
very least, I am advised to use this only on POD classes...). I have
this POD class that I want to add methods, and thus have to replace
parts of my code where I have to call std::fwrite.
For example, I have
struct foo {
int a;
char b;
};
// ...
foo f;
FILE* file;
std::fwrite (&f, 1, sizeof(f), file);
// ...
If I add a method to foo, how would I change the code snippet?
只要没有虚拟方法或非pod成员,它应该是
仍在工作。但我会给它正确的流媒体运营商。
朋友std :: ostream& operator<<(std :: ostream& out,const foo& f);
friend std :: istream&运算符>>(std :: istream& out,foo& f);
Ian
As long as there are no virtual methods or non pod members, it should
still work. But I''d give it proper streaming operators.
friend std::ostream& operator<<( std::ostream& out, const foo& f );
friend std::istream& operator>>( std::istream& out, foo& f );
Ian
Ian写道:
kelvSYC写道:
kelvSYC wrote:
foo f;
FILE *文件;
std :: fwrite(& f,1,sizeof(f),file) ;
foo f;
FILE* file;
std::fwrite (&f, 1, sizeof(f), file);
....只要没有虚拟方法或非pod成员,它应该仍然有效。但我会给它正确的流媒体运营商。
朋友std :: ostream&运算符<<(std :: ostream& out,const foo& f);
.... As long as there are no virtual methods or non pod members, it should
still work. But I''d give it proper streaming operators.
friend std::ostream& operator<<( std::ostream& out, const foo& f );
....这可能与
fwrite(& f,sizeof(f),1,file);
.... which probably isn''t quite the same as
fwrite (&f, sizeof(f), 1, file);
Rapscallion写道:
Rapscallion wrote:
Ian写道:
Ian wrote:
kelvSYC写道:
kelvSYC wrote:
foo f;
FILE * file;
std :: fwrite(& f,1,sizeof(f),file);
foo f;
FILE* file;
std::fwrite (&f, 1, sizeof(f), file);
...
...
只要没有虚拟方法或非pod成员,它应该仍然有效。但我会给它正确的流媒体运营商。
朋友std :: ostream& operator<<(std :: ostream& out,const foo& f);
As long as there are no virtual methods or non pod members, it should
still work. But I''d give it proper streaming operators.
friend std::ostream& operator<<( std::ostream& out, const foo& f );
...这可能与
fwrite(&)完全相同; f,sizeof(f),1,file);
... which probably isn''t quite the same as
fwrite (&f, sizeof(f), 1, file);
是的,这就是为什么我建议免费的运营商!
Ian
True, that''s why I suggested complimentary operators!
Ian
这篇关于结构布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!