|
-
September 30th, 2005, 06:12 AM
#1
stl structure which is suitable for inheritence
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
-
September 30th, 2005, 06:16 AM
#2
Re: stl structure which is suitable for inheritence
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.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
September 30th, 2005, 06:22 AM
#3
Re: stl structure which is suitable for inheritence
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 30th, 2005, 11:02 AM
#4
Re: stl structure which is suitable for inheritence
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.
-
September 30th, 2005, 11:31 AM
#5
Re: stl structure which is suitable for inheritence
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.
 Originally Posted by lambda32
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.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 30th, 2005, 05:30 PM
#6
Re: stl structure which is suitable for inheritence
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|