虽然我检查了while条件中的EOF,但 while 循环运行了无限次。但它仍然运行了无限次。下面是我的代码:
int code;
cin >> code;
std::ifstream fin;
fin.open("Computers.txt");
std::ofstream temp; // contents of path must be copied to a temp file then renamed back to the path file
temp.open("Computers.txt", ios_base::app);
string line;
string eraseLine = to_string(code);
while ( getline(fin, line) && !fin.eof() ) {
if (line == eraseLine)
{
/*int i = 0;
while (i < 10)
{*/
temp << "";
//i++;
//}
}
if (line != eraseLine) // write all lines to temp other …Run Code Online (Sandbox Code Playgroud) 我做了以下 3 个类:
struct Parent1
{
virtual void f()
{
cout << "\nParent1::f";
}
};
struct Parent2
{
virtual void g()
{
cout << "\nParent2::g";
}
virtual void z()
{
cout << "\nParent2::z";
}
};
struct Child : public Parent1, public Parent2
{
virtual void h()
{
cout << "\nChild::h";
}
};
Run Code Online (Sandbox Code Playgroud)
在 main 中,当我调用 的函数z时Parent2,它会调用h子类的函数。为什么会这样?以下是main函数:
int main()
{
Child obj;
Parent2 * p2 = (Parent2*)(Parent1*)&obj;
p2->z();
return 0;
}
Run Code Online (Sandbox Code Playgroud)