Advertisement

Polymorphism and iterator-based lists

Started by October 03, 2000 04:37 PM
1 comment, last by Remnant 24 years, 3 months ago
I'm running into a little problem lately, that I'd like to hear if anyone has a solution. I've created a linked list class patterned after the STL. It uses iterators inside of the template'd class to be able to move through the list. The problem is this : I want to be able to substitute List* for List*. If this was a regular C array, that would be no problem if Part is derived from Engine. However, MSVC++ complains about not being able to convert from List* to List* . Logically this should be doable, but I can see that its because the type 'List' is NOT derived from 'List', even though they contain parts that ARE derived from each other. Basically, I want my container class to allow polymorphism. How should I go about this? EDIT: Argh, the BBS is mistaking the angle brackets for HTML. There's Engine and Part inside of angle brackets after the List - Remnant
- (Steve Schmitt)
Edited by - Remnant on 10/3/00 4:40:07 PM
- Remnant- (Steve Schmitt)
first, to get an > or < you type & g t or & l t (without the spaces)

second, to make somehting act like an array you overload the [] operator and return a data reference

suppose
        template<class T>class LinkedList{T& operator[](int i); //rip through list, return i'th element//or maybe you should put the []operator in the iterator class}main(){LinkedList<float> list;list.Add(1.0f);list.Add(5.0f);list.Add(3.14159265358979323f);float a,b,c;a = list[0]; //a = 1.0b = list[2]; //b = 3.1415....c = list[1]; //c = 5.0}        



third, thats a bad idea
typically with list, you use the iterator and do a getfirst, getnext, getnext, getnext,... until you exhaust the list. This way you go through the list as efficiently as possible. With the [] opeartor, you gotta tranverse the list each time its called. I guess you could make it smart, and remember where you are, cause odds are, afer asking for element i, they're gonna ask for element i+1...


fourth, thats not polymorphism. thats operator overloading. (i guess acedemia'ly speaking its a form of polymorphism, but its not C++/OOP type polymorphism.)

Edited by - Magmai Kai Holmlor on October 3, 2000 10:24:59 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Mag : yes, I know. Thanks for the answer, but you describe how to make an array-based access to the list -- what I''m wondering about is how to make my list compatible with polymorphic types. let me try again.


I have a ship that has several lists. One list contains Engines, another contains Shields, and so on.

All of those types derive from the base class Part.

I want to be able to have a List< Part >::Iterator be able to accept list-iterators of types derived from part.

I''ll use an example:

    void DamagePartList(List<Part> *parts) {  for (List<Part>::Iterator cur = parts.begin(); cur != parts.end; cur++) {    cur->doDamage(someamount);  }}where I have classesclass Part { ...  virtual void doDamage(int amount);}class Shield : public Part ...class Engine : public Part ...and the calling code should be able to dovoid Ship::do_damage() {   // Ship has List<Engine> Leng;   // Ship has List<Shield> Lshld;  DamagePartList(Leng);  DamagePartList(Lshdl);...}    



In short, I want a iterator for a base class to also work for derived classes.

I''m able to achieve this now by reinterpret_casting the pointer blindly, since the list iterator size or data structure does not change, but this smacks of a hack. I need a cleaner way to do this.

-- Remnant

This topic is closed to new replies.

Advertisement