【发布时间】:2017-05-10 04:57:06
【问题描述】:
此处的代码用于创建学生成绩单项目。在尝试理解时,我们无法弄清楚以下代码的用途和功能:
File.read(reinterpret_cast<char *> (&st), sizeof(student));
int pos=(-1)*static_cast<int>(sizeof(st));
File.read(reinterpret_cast<char *> (&st), sizeof(student));
if(st.retrollno()==n)
{
st.showdata();
cout<<"\n\nPlease Enter The New Details of student"<<endl;
st.getdata();
int pos=(-1)*static_cast<int>(sizeof(st));
File.seekp(pos,ios::cur);
File.write(reinterpret_cast<char *> (&st), sizeof(student));
cout<<"\n\n\t Record Updated";
found=true;
}
【问题讨论】:
-
学生是什么?此代码将其读取为原始二进制文件。如果学生类型不是标准布局,则为 UB。它也有字节序的问题
-
您提出问题的方式表明您并不真正了解
reinterpret_cast和static_cast的含义。
标签: c++ pointers reinterpret-cast static-cast