Original Post
Ok I've recently started to write my own generic (templated) linked-list class for use in my games, and I need some help...
Ok, firstly I'll show you my class and my node structure(incomplete):
(and please, don't tell me to just use the std::list instead. Yes yes, I know it would be much easier and a 100 hundred times better than what I could write, but I want to write this list class for my own experience to have a good understanding of linked-lists)
Thanks for your time!
[edited by - chacha8 on July 19, 2003 12:46:06 PM]
template <class Object>
struct Node
{
Object data; //Actual node data
Node *next; //Ptr to next node in list
};
//////////////////////////////////////////////////////
template <class Object>
class List
{
public:
//Constructor and destructor
List(): head(NULL) {};
~List();
void InsertFront(Object &object);
void InsertEnd(Object &object);
void DeleteNode(Object &key);
//void DrawList()... <---- NEED HELP WITH THIS METHOD
//Accessor methods
int NumNodes() { return numNodes; };
bool IsEmpty() { return (head == NULL); };
private:
Node<Object> *head; //Pointer to the head node
static int numNodes; //To count number of nodes in list
};
Ok my problem is how I will call the methods of the Generic "Object" type? For example, just say I had a List of asteroids and I want to traverse the list and call a "draw" method on each node in the list. I was thinking of having a method, List::DrawList, and passing in a pointer to the "Object" draw method like so:
template <class Object>
void List<object>::DrawList( void (Object::*pMemFunc)() )
{
//Search through list and call pMemFunc on each node like this:
currentNode->pMemFunc();
}
//And in the case that the "Object" type is not a class:
template <class Object>
void List<object>::DrawList( void (*pFunc)(Object &object) )
{
//Search through list and call pFunc like this
pFunc(currentNodeToDraw);
}
I tried this all out and I couldn't get it working, but I know that there is a better way to accomplish this, anyone got any ideas/advice?? (I was thinking of maybe using inheritance, as someone mentioned that in another thread about generic linked-lists, but I don't have a clue as to implementing inheritance in this problem).
ANY help would be much appreciated