我正在使用OOP进行文件处理,并且已经了解了很少的内置函数read()
& int*
但我不知道为什么需要施法。我也试过投射到char*
而不是#include<iostream>
#include<fstream>
using namespace std;
class Person{
private:
int age;
char name[15];
public:
void getData()
{
cout<<"Enter the name of person:";
cin>>name;
cout<<"Enter the age of person:";
cin>>age;
}
void showData()
{
cout<<"\nName is:"<<name;
cout<<"\nAge is:"<<age;
}
};
int main()
{
Person p;
p.getData();
ofstream outfile("Person.txt");
outfile.write(reinterpret_cast<char*>(&p), sizeof(p)); // how this works?
ifstream infile("Person.txt");
infile.read(reinterpret_cast<char*>(&p), sizeof(p)); // how this works?
p.showData();
return 0;
}
然后投射不起作用。我尝试使用谷歌搜索但无法找到好的教程或文章。
$scope.menu = [];
angular.forEach(data, function(value, key) {
if (value.ParentId == 0){
$scope.menu.push(value);
}
});
angular.forEach( data, function(value, key) {
if (value.ParentId != 0){
angular.forEach($scope.menu, function(value2, key2) {
if (value.ParentId == value2.MenuId){
if(value2.children == undefined){
value2.children = [];
value2.showChildren = false;
}
value2.children.push(value);
}
});
}
});
你能告诉我为什么需要Casting吗?为什么sizeof运营商?