Click to See Complete Forum and Search --> : stl structure which is suitable for inheritence
lambda32
September 30th, 2005, 06:12 AM
hi,
I have searched around this forum for an stl structure which is good for classes which inherits from another class.
I am no doubt a beginner at c++ so pardon if this has been asked a lot of time.
I learnt that if I use Vector<Person> stor;
and i use: stor.push_back(teacher);
I would lose the methods that teacher has, when I extract the object from the vector using Iterator. The original object has been sliced.
Any suggestion, help and ideas are greatly appreciated. I have done a lot of searching and I still don't understand.
Thank you for your help and consideration in advance.
regards,
Arvy
Kheun
September 30th, 2005, 06:16 AM
You should use "Vector<Person *> stor" instead. However, if you create the object use new, don't forget to delete when it is not needed.
Graham
September 30th, 2005, 06:22 AM
The problem is not in the STL classes, it's in how you are attempting to use them. This is more a function of the rules of C++, than of the STL.
If you wish to store polymorphic objects in an STL container, you must store pointers to those objects, not the objects themselves. This rule follows from the rules of C++: arrays (and, therefore, vectors) can only store objects of the same size; derived class objects can be bigger than base class objects, so an attempt to store a derived object in a base object will result in any "extras" being snipped off. Since virtual function overrides in the derived class may depend on some of those extras, virtual function lookup stops at the base class versions (which can't depend on any derived-class-only members). Thus, if you try to assign a derived object to a base object, you will throw away everything that makes it derived and leave the underlying base object to be copied.
To get over this, we store pointers to the objects in the vector. Since pointers are always the same size, there is no problem with storing a pointer-to-base next to a pointer-to-derived next to a pointer-to-derived2, etc. Type safety rules stop you from storing a pointer-to-unrelated, though.
lambda32
September 30th, 2005, 11:02 AM
Thanks very much for your replies.
Now that I understand this, how can I use stl vector to store different objects so that I don't have to typecast it when I want to obtain the object.
So for example, I don't want to do: teacher* a = *(personTraverser)
where: personTraverser is an iterator.
Is there any better data structure for this purpose? For example if I wanted to use visitor pattern, which requires the objects to be in its final type.
SuperKoko
September 30th, 2005, 11:31 AM
In a normal program you should design the base class so you only manipulate the pointer to the base class.
To do so, the base class should contain virtual functions, and you should use polymorphically this base class.
Exceptionally, you can use dynamic_cast to convert a pointer to a base class to a pointer to a derived class. It returns NULL if the object is not of the derived type.
However, doing so, generally reduces the maintainability and extensibility of the program.
Is there any better data structure for this purpose? For example if I wanted to use visitor pattern, which requires the objects to be in its final type.
What do you want to visit exactly?
If you want to display all the objects, for example, you should put a Display virtual method in the base class.
In practice, there are very few programs where dynamic_cast is really needed.
lambda32
September 30th, 2005, 05:30 PM
Well, for example: if I there's a couple object in the vector say:
student, student, teacher, student, adminstrator, teacher.
I wanted to get the first teacher object to do something(which is general to everybody) such as goToWork().
How would I access that first teacher object without using any dynamic casting and downcasting?
regads,
Arvy
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.