Archived

This topic is now archived and is closed to further replies.

Polymorphism and iterator-based lists

This topic is 6500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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
  templateclass 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 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}

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

Share on other sites
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 *parts) { for (List::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 Leng; // Ship has List 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

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×