Jump to content
  • Advertisement

Archived

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

Remnant

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.0

b = 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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 classes

class Part { ...
virtual void doDamage(int amount);
}

class Shield : public Part ...
class Engine : public Part ...

and the calling code should be able to do

void 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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!