How to have object specific functions in a generalised linked list

Started by
3 comments, last by Kyo 20 years, 12 months ago
I have a linked list which uses templates for generalising what the list can hold, but what if I want to hold a list of CBomb for example, and I want to add a function called drawBombs(HDC hdc) to the list so it draws every bomb?
  
template <class T>
class linkedList
{
public:
	linkedList();
	~linkedList() { delete myHead; }

	void insert(T *theObject);
	void showAll() { myHead->show(); }
	T* find(int value) { return myHead->find(value); }
	void remove(int value) { if (value != 0) { myHead->remove(value); } }

private:
	headNode<T> *myHead; 

};
  
I was thinking of deriving the linked list class and including the drawBomb(HDC hdc) function, but then i''d have to derive the node ADT class, the tailNode class, the headNode class and the internalNode class.... Or I was also thinking of adding moveNextNode() which would return a pointer to the next node, that way I can just loop through every single node and call the drawBomb function one by one.
Advertisement
it''s all about std::list and std::for_each
Thanks but i''m leaving std (what is it exactly? Is it the same as STL? What''s the difference?) for later.

I would like to know what I should do, as this is my first time implementing a linked list which wasn''t too hard but I can''t really find much info on how one is supposed to actually use it.
Nevermind I just found a way: I added a getNext() function to the linked list which would return the object held in the next node, and moveFirst() function when its moved through all the nodes, so i can access each node in the list serially.

quote:Original post by Kyo
I have a linked list which uses templates for generalising what the list can hold, but what if I want to hold a list of CBomb for example, and I want to add a function called drawBombs(HDC hdc) to the list so it draws every bomb?

Don''t do that. You have to add functionality to your linked-list for traversing subsequences of the list. In the case of C++, that means the half-assed iterator idiom, which I''m sure you will find lots about via Google.
quote:
I was thinking of deriving the linked list class and including the drawBomb(HDC hdc) function, but then i''d have to derive the node ADT class, the tailNode class, the headNode class and the internalNode class....

That''s because the design of your linked-list class is flawed. But that''s OK, because you''re beginning to see that it''s flawed. Some reading around the subject would help save you effort in future (for all manner of things). There''s *lots* of information about linked-lists.
quote:
Or I was also thinking of adding moveNextNode() which would return a pointer to the next node, that way I can just loop through every single node and call the drawBomb function one by one.

Yes, good idea. You''re on your way to discovering iterators.
quote:
Thanks but i''m leaving std (what is it exactly?

It''s the name of a namespace where all of the C++ Standard Library lives. You should really know about this if you are programming in C++. It''s a bit like a C++ programmer saying "what''s all this stdlib.h, stdio.h and stuff?"
quote:
Is it the same as STL?

No, but it does contain some elements of the STL.
quote:
[...] for later.

Don''t leave it for later. If you hadn''t have left it ''til later then you wouldn''t be wasting your time writing incorrect linked-list classes and trying to figure out why they won''t do what you want.

This topic is closed to new replies.

Advertisement