<分区>
我正在为我的系统编程类(class)做家庭作业。 我必须实现大学模拟。 我有一个 Course 抽象类和它的子类 ComputerScienceCourse(以及其他一些不影响我要问的问题的子类)。 我有一个 Student 抽象类和它的子类 ComputerScienceStudent(以及其他一些不影响我要问的问题的子类)。 在 ComputerScienceCourse 子类中有一个函数:
void teach(){
for(i = all students that take this course [stored in a vector of pointers to Student objects, which is a member field of the CSCourse child class, called students]){
this->students.at(i)->study(*this);
}
}
此函数为所有上过这门课的学生调用学习函数。该函数是子类CSStudent的成员函数
void study(Course &c){
if(this->failedclass){
c.removeStudent(this)
}
}
现在,当我检查输出时,teach 函数中的循环不会为其中的所有学生调用 study 函数。 例如,如果我有 4 个学生在做这门类(class),有时它会只调用前三个学生的学习功能,有时它会调用第一个和最后一个学生的功能......各不相同。 是什么导致 for 循环不调用所有学生的学习函数?! 以下是教学功能的完整代码:
for(unsigned int i=0; i<this->studentMembers.size(); i++){
this->studentMembers.at(i)->study(*this);
}