stl list<>::iterator for derived classes ?!?
-
Hi i have a little problem using the list<>::iterator. Assume you have a class CARS{ virtual void info();...} and some public derived classes as GM{void info();...},BMW{void info();...}
... list<CARS> carpark; GM car1(); GM car2(); BMW car3(); BMW car4(); carpark..push_back(car1); carpark..push_back(car2); carpark..push_back(car3); carpark..push_back(car4); list<CARS>::iterator it; for(it=carpark.begin();it != carpark.end(); it++) { cout << it->info() << endl; } ...
so far so fine. Now how to call the info() from GM or BMW using the iterator ?? (not the one from CARS) THX Tim -- modified at 17:50 Thursday 5th January, 2006 -
Hi i have a little problem using the list<>::iterator. Assume you have a class CARS{ virtual void info();...} and some public derived classes as GM{void info();...},BMW{void info();...}
... list<CARS> carpark; GM car1(); GM car2(); BMW car3(); BMW car4(); carpark..push_back(car1); carpark..push_back(car2); carpark..push_back(car3); carpark..push_back(car4); list<CARS>::iterator it; for(it=carpark.begin();it != carpark.end(); it++) { cout << it->info() << endl; } ...
so far so fine. Now how to call the info() from GM or BMW using the iterator ?? (not the one from CARS) THX Tim -- modified at 17:50 Thursday 5th January, 2006It should just be called for you automatically. That's the point of an overrided method. However, if all cars have a type, then it should be stored in a variable that exists in the base class, and the GetType method should exist only in the base class. The derived classes should just set that variable. Christian Graus - Microsoft MVP - C++ -- modified at 17:58 Thursday 5th January, 2006
-
It should just be called for you automatically. That's the point of an overrided method. However, if all cars have a type, then it should be stored in a variable that exists in the base class, and the GetType method should exist only in the base class. The derived classes should just set that variable. Christian Graus - Microsoft MVP - C++ -- modified at 17:58 Thursday 5th January, 2006
-
Hallo THX so far, but how do i do this with the GetType Methode and how can this help me calling the method of the derived class ?? Tim
#include class CAR { private: std::string type; public: std::string GetType(); } class BMW { public: BMW() { type = "BMW"; } } and so on. you don't need to use std::string, obviously. Christian Graus - Microsoft MVP - C++
-
#include class CAR { private: std::string type; public: std::string GetType(); } class BMW { public: BMW() { type = "BMW"; } } and so on. you don't need to use std::string, obviously. Christian Graus - Microsoft MVP - C++
-
Well, my way, there is no individual info method. However, in either case, you should find you're calling it in your original code. If not, perhaps you need to store a pointer instead of a class instance, perhaps it's being downcast by std::list at the moment ? Christian Graus - Microsoft MVP - C++
-
Well, my way, there is no individual info method. However, in either case, you should find you're calling it in your original code. If not, perhaps you need to store a pointer instead of a class instance, perhaps it's being downcast by std::list at the moment ? Christian Graus - Microsoft MVP - C++
-
OK THX alot Will write my own list and iterator That might be no problem then i just liked to know how to use list and iterator CU
tbrake wrote:
Will write my own list and iterator
No, please don't do that. What I was suggesting was that you should perhaps try storing a CAR * instead of a CAR in std::list. Christian Graus - Microsoft MVP - C++
-
Hi i have a little problem using the list<>::iterator. Assume you have a class CARS{ virtual void info();...} and some public derived classes as GM{void info();...},BMW{void info();...}
... list<CARS> carpark; GM car1(); GM car2(); BMW car3(); BMW car4(); carpark..push_back(car1); carpark..push_back(car2); carpark..push_back(car3); carpark..push_back(car4); list<CARS>::iterator it; for(it=carpark.begin();it != carpark.end(); it++) { cout << it->info() << endl; } ...
so far so fine. Now how to call the info() from GM or BMW using the iterator ?? (not the one from CARS) THX Tim -- modified at 17:50 Thursday 5th January, 2006OK, I've done some testing: this works: #include #include #include class CAR { public: virtual char * GetInfo() { return "BASE"; }; }; class BMW: public CAR { char * GetInfo() { return "BMW"; } }; class Gemini :public CAR { char * GetInfo() { return "Gemini"; } }; class Benz :public CAR { char * GetInfo() { return "Benz"; } }; int _tmain(int argc, _TCHAR* argv[]) { std::list cars; cars.push_back(new BMW()); cars.push_back(new BMW()); cars.push_back(new Gemini()); cars.push_back(new Benz()); std::list::iterator it = cars.begin(); while(it != cars.end()) { std::cout << (*it)->GetInfo() << std::endl; ++it; } int i; std::cin >> i; return 0; } but if you don't store pointers in the list, then it does indeed downcast to the base class. Christian Graus - Microsoft MVP - C++