0
我想动态分配一个基(学生)类的数组,然后指派派生(数学)类的指针到每个数组插槽。我可以通过创建一个指向基类的指针,然后将其分配给派生类,但是当我尝试将指针指定给动态分配的基类数组时,它会失败。我已经发布了我在下面使用的代码片段。所以基本上我的问题是,为什么不动态分配的工作?动态分配类,与继承问题
Student* studentList = new Student[numStudents];
Math* temp = new Math(name, l, c, q, t1, t2, f);
studentList[0] = temp;
/*Fragment Above Gives Error:
main.cpp: In function âint main()â:
main.cpp:55: error: no match for âoperator=â in â* studentList = tempâ
grades.h:13: note: candidates are: Student& Student::operator=(const Student&)*/
Student * testptr;
Math * temp = new Math(name, l, c, q, t1, t2, f);
testptr = temp
//Works
好吧,我没有在C看到这++我从某处学习的书,所以我必须审查。 – dubyaa 2010-11-17 00:03:44
'new Student * [numStudents];'为什么有指针?这是我不明白的唯一部分。 – dubyaa 2010-11-17 00:04:58
@dubyaa:因为你需要创建一个项目数组,每个项目都是'Student *'。 – 2010-11-17 00:08:57