Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. stl list<>::iterator for derived classes ?!?

stl list<>::iterator for derived classes ?!?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tbrake
    wrote on last edited by
    #1

    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

    C 2 Replies Last reply
    0
    • T tbrake

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • C Christian Graus

        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

        T Offline
        T Offline
        tbrake
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • T tbrake

          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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          #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++

          T 1 Reply Last reply
          0
          • C Christian Graus

            #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++

            T Offline
            T Offline
            tbrake
            wrote on last edited by
            #5

            HI OK have somthing like this but how to call the individual info() method ?? Tim

            C 1 Reply Last reply
            0
            • T tbrake

              HI OK have somthing like this but how to call the individual info() method ?? Tim

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              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++

              T 1 Reply Last reply
              0
              • C Christian Graus

                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++

                T Offline
                T Offline
                tbrake
                wrote on last edited by
                #7

                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

                C 1 Reply Last reply
                0
                • T tbrake

                  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

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  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++

                  1 Reply Last reply
                  0
                  • T tbrake

                    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

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    OK, 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++

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups