Thread: inherited classes and stl container

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    inherited classes and stl container

    Hi, i was working on simple hospital program.

    I got a patient base class,and from the base class i got three types of patients classes.(namely in-patient,day-patient,out-patient).

    I got a doctor class,and i need to use a stl container to store the patients treated by the doctor.the doctor treats all 3 types of patients?
    i tried vector,but vector allows me to to store 1 type of patient only.

    what type of stl container to use so i can store all the 3types of patients? can some one guide ?

    Thanking you very much,
    Rahul SK

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Polymorphism works with pointers (to the base class), which you should store in the vector.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    anon,thanx for your reply.but i am still confused.
    do you mind to give me some example ?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Code:
    class base {
    public:
    ...
    };
    
    class derived : public base {
    ..
    };
    
    std::vector <base> m_list;

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The last line should be:
    Code:
    std::vector <base*> patient;
    if you want polymorphism to work. Which seems to be your goal, as you probably want to do something like this regardless of the type of the patient:
    Code:
    patient[x]->getTreatment(doctor);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Managing vectors of pointers is somewhat unreliable. A better choice would be a Boost.PtrContainer ptr_vector.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    Managing vectors of pointers is somewhat unreliable. A better choice would be a Boost.PtrContainer ptr_vector.
    Whats the difference betwen them?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    ptr_vector manages the lifetime of the pointed-to objects automatically. You don't have to call delete yourself.
    At the same time, it avoids the overhead of shared_ptr that would get if you used a vector of shared_ptrs, which is often not necessary.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    So when you have to work with polymorphism its better to use ptr_vector instead of shared_ptr vector?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It depends - does anyone apart from the container ever "own" the object? Then use shared_ptr.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. STL List container with classes
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2005, 02:13 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM