Archived

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

Linked List Where Each Node Has A Pointer To A Different Data Type

This topic is 4982 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

Hi There, I have searched through books, websites (such as this one and others) and google etc. but I am still unable to find an answer to this problem, I hope someone here can help! I am creating a linked list where each node has a pointer to an object. The problem is that I have 7 different types of object to point to and all of them are to be stored in the same linked list. Therefore a linked list that stores pointers to only one type of object will not work. All the books/tutorials/etc. I have read through explain how to make a linked list where you can specify the data type at run-time using 'template' etc., but the list is made up of all the same data type. Does anybody know how I can create a linked list like this or can you point me in the right direction? Thanks, Goose Serbus. P.S. Before anyone asks I can't use STL or anything like that because this is for a school project (I'm not asking you to do my homework for me just for a little advice), also I understand all about linked lists and their fundamentals etc. so I don't need any help with that, thanks! [edited by - gooseserbus on April 21, 2004 7:12:20 AM]

Share this post


Link to post
Share on other sites
An easy (but ugly) way to do it is to have the node contain a void pointer and an integer value which tells what kind of type the node is pointing at


if (type == TYPE_ID_MONSTER)
Monster *monster = (Monster*)pointer;
if (type == TYPE_ID_FAIRY)
Fairy *fairy = (Fairy*)pointer;



A yet better way is to inherit classes with virtual functions


class Entity
{
public:
virtual void move() = 0; //Some common functions

};

class Monster : public Entity
{
private:
int x,y;
public:
void move() {x += 2;} //This is what the monster do to move

};




So when retreiving the object, just convert it to an Entity, and use the virtual functions.


Monster *m = linkedlist.getnext();
m->move(); //Will be executed differently depending on what it actually is. A monster, Fairy, brick wall or anything.



Electron

"The truth can be changed simply by the way you accept it."

"''General failure trying to read from file'' - who is General Failure, and why is he reading my file??"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If they''re all derived from a base class then you can simply store base pointers.

If they aren''t related in any way then I wonder why you need to store them all in one list in the first place. Ask yourself how you will make use of the data stored in the list. It is quite likely you''ll be thinking of casting or having an ID that identifies what type of object it is. When your design gets like this then you can be pretty sure it''s not good.

Anyway, unrelated classes can be wrapped in a templated class stored according to the base class again, but I''m not sure whether this will lead to a bad design for you.


struct Anything {
};
template<typename T>
struct AnythingWrapper : public Anything {
T* object;
Wrapper(T* obj) : object(obj) {
}
};

template<typename T>
AnythingWrapper<T>* createWrapper(T* obj) {
return new AnythingWrapper<T>(obj);
}

std::vector<Anything*> wrappers;
wrappers.push_back(createWrapper(new int(42)));
wrappers.push_back(createWrapper(new double(3.157)));
wrappers.push_back(createWrapper(new std::string("hello")));


This is just an indication of what you could do. It''s not industrial strength at all and has horrible memory allocation issues.

You might do better looking at <a href="http://boost.org/">boost</a>.<a href="http://boost.org/doc/html/any.html">any</a>. But as noted it could lead to a very bad design.

Share this post


Link to post
Share on other sites
First off thanks for the replies.

The point that I noticed was about them all being derived from a base class:

quote:
Original post by Anonymous Poster
If they''re all derived from a base class then you can simply store base pointers.


So I did the following:



LinkedList myLinkedList; // create my linked list

SubClassObject myObject; // create my sub-class object

myObject.inputSomeData( 675 ); // put some info into that object


BaseClassObject *myBaseClassPointer; // create a pointer to my base-class object

myBaseClassPointer = &myObject; // point the pointer to my sub-class object

myLinkedList.addToList( myBaseClassPointer ); // add the object to the list via my base-class pointer




And it worked fine! Thanks for pointing me in the right direction I had not realised I could do this but now it seams obvious.

Goose Serbus.

Share this post


Link to post
Share on other sites
quote:
Original post by gooseserbus

BaseClassObject *myBaseClassPointer; // create a pointer to my base-class object
myBaseClassPointer = &myObject;



This is unnecessary. Any derived-from pointer is also a baseclass pointer. You can just do myLinkedList.addToList( &myObject );


--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites