Writing a generic method that prints any object

Hi,
I have a Generic LinkedList class. It contains a display method, which prints the value of all the nodes in the linked list. Node contains a 'value' which is of type given by user, and pointer to next node.

Now, I want a display method to print any object because the user can make LinkedList of his own class objects.

I cannot overload << in my LinkedList class, because I wont know about the object that user will use in the Nodes

Thanks!
I cannot overload << in my LinkedList class, because I wont know about the object that user will use in the Nodes

You can overload operator<< for your LinkedList class and require the user to implement a similar overload for whatever they want to store in the LinkedList if they want to use that functionality.

Of course, it may be better to supply a way for a user of the class to access each element of the list and do something with it. That's a more general solution that also solves the printing problem.
Topic archived. No new replies allowed.