[英]QList pointer function to abstract class
我問這個問題很愚蠢,因為它看起來很簡單,但我不知道該怎么做,也無法在互聯網上的任何地方找到它。 我正在嘗試創建一個將QList返回到標准輸出的函數,指向抽象類的指針使我感到困惑。 AbstractStudent類生成另一個類Student的實例。 這是函數:
QList<AbstractStudent*>* StudentList::returnList() const{
}
存儲抽象類的指針的列表將能夠存儲指向該抽象類的任何子類的指針。
考慮以下幾點:
AbstractStudent.h:
class AbstractStudent
{
// ...
};
Student.h:
class Student : public AbstractStudent
{
// ...
};
任何其他類.cpp:
QList< AbstractStudent* > studentList;
// Each of the following works:
AbstractStudent* student1 = new Student( /* ... */ );
studentList.append( student1 );
Student* student2 = new Student( /* ... */ );
studentList.append( student2 );
Student* student3 = new Student( /* ... */ );
AbstractStudent* student3_1 = student3;
studentList.append( student3 );
但是,我對您的最后一句話感到有些困惑,聲稱AbstractStudent生成Student對象。 我本來希望Student繼承AbstractStudent,而其他一些類會生成Student對象,例如在我的示例中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.