想要將不同的形狀對象存儲到文件中並使用cpp讀取相同的對象。請幫我怎麼做?如何從cpp中的不同基類對象的文件中讀取對象
static void saveShape(Shape* shape)//Shape may be circle, square, rectangle.....
{
ofstream os;
os.open("shapetemp.txt", ios::app);
os << shape;
os.close();
}
static void readShape()
{
Shape tempShape ;
ifstream fin;
fin.open("shapetemp.txt",ios::in);
while (??)
{
//what is code to write here??
}
}
}
請格式化你的代碼正確。 – px06
你的問題是什麼? – UnholySheep
您將不得不從文件中讀取實際的類型,而不僅僅是基類。 –