Message Board
Message Board > C/C++ > Polymorphism and lists |
May 2, 2007, 20:38 | |
Frimkron
Frustrated Megalomaniac 703 posts |
Ok I have a question. In java I can create a list of objects using an abstract type as the type for the list and then add different flavours of subclass to the list as I like. For example Code: //abstract game object superclass abstract class GameObject { public abstract void draw(); } //some game object class Player extends GameObject { public void draw() { //draw code here } } //another game object class Tree extends GameObject { public void draw() { //draw code here } } //Main class class Main { //create a list with abstract type List<GameObject> objectList = new ArrayList<GameObject>(); //add some objects objectList.add(new Tree()); objectList.add(new Player()); //iterate over the different objects refering to each //as a GameObject Iterator it = objectList.iterator(); while(it.hasNext()) { GameObject obj = it.next(); obj.draw(); } } My question then is this. I wrote a list-like thing in c++ using a template, but then realised that the inner class I was using to store each list item was holding the item as.. like... on the stack, not on the heap. Like: Code: class ListItem { T value; }; So this meant when I came to try and use a pure virtual class as the type for the list, the compiler whined that it couldn't instantiate the class. The reason I did it this way rather than using a pointer, i.e. Code: class ListItem { T* pValue; }; was because when you add to the list it takes as input a reference to the new item and uses it to make its own copy to store in the list: Code: void insert(T& newItem) { slots[nextSlot()] = newItem; } something like that. The STL linked list seemed to do the same - take a copy of the item you pass in. It makes sense, because if instead it only stored a pointer to the item you pass in, you could pass in something stupid like a local variable that would cease to exist and leave a bad pointer. But it would seem that using pointers would be the only way to do something similar to the java list, where the abstract class isn't instantiated but just used to refer to classes which extend from it. It seems a bit dangerous though. Is that just the price you pay? ____________ |
# |
May 2, 2007, 20:39 | |
Frimkron
Frustrated Megalomaniac 703 posts |
Christ somebody fix these code tags! Oh wait that's my job. ____________ |
# |
May 2, 2007, 21:12 | |
Quiest
now with more happynes 142 posts |
The code tags here suck anyways, I hate the board pasting empty lines inbetween the lines of empty code... any sense in this? @topic: can`t tell you much, but from as far as my knowledge goes atm, pointers are the tool to use. ____________ Roundhousekick to the face, baby! |
# |
May 2, 2007, 22:44 | |
Fiona
games are terrible -9616558 posts |
Quoting Quiest: The code tags here suck anyways, I hate the board pasting empty lines inbetween the lines of empty code... any sense in this? there's no sense, but there's a reason WHY it happens i'm sorry, oversight when i did the site ____________ laffo |
# |
May 4, 2007, 00:33 | |
Frimkron
Frustrated Megalomaniac 703 posts |
Hmm well I kinda realised that you can of course have a list where the type of item held is actually a pointer - which makes sense - then it means its up to the one using the list to use it wisely. So I can store a pointers to my abstract game objects in my list and all is well.
____________ |
# |
May 5, 2007, 11:26 | |
danko
Whiskered 31 posts |
Quote: But it would seem that using pointers would be the only way to do something similar to the java list, where the abstract class isn't instantiated but just used to refer to classes which extend from it. It seems a bit dangerous though. Is that just the price you pay? Yes, dynamic binding is only possible using pointers. Methods of the bound class which will be called from references or pointers must be declared using "virtual". ____________ |
# |
Message Board > C/C++ > Polymorphism and lists