旅游和导游。GuideTour 扩展了 Tour。我创建了这些项目的列表并将它们添加到向量中。
list = new vector<Tour>();
list->push_back(Tour("FP001", "Fun Park 3 Day Pass", 110.00));
list->push_back(Tour("BG002", "Botanical Gardens Entry Pass", 30.00));
list->push_back(GuidedTour("SK003", "Learn to Ski Adventure Tour", 240.00, "28/07/2008", "Zail S", 25));
list->push_back(Tour("OZ004", "Open Range Zoo Entry Pass", 45.00));
list->push_back(GuidedTour("AB005", "Abseiling for Beginners Tour", 120.00, "15/07/2008", "Rex P", 35));
list->push_back(GuidedTour("RA006", "White Water Rafting Tour", 200.00, "22/06/2008", "Clint R", 16));
然后我想通过这个数组并检查这些对象的类型
void TourManager::callDisplayOnEach() {
for (vector<Tour>::iterator it = list->begin(); it != list->end(); ++it) {
if (typeid(*it) == typeid(GuidedTour)) {
cout << "Guided Tour" << "\n";
}
else {
cout << "NOT Guided Tour : " << typeid(*it).name() << "\n";
}
//(*it).display();
}
}
但是,它始终返回 NOT 导游选项。