Accessing a function of an object of type void*

Hello,

I am creating a doubly-linked list using two classes, one is the MyList class and one the MyListElement class. The MyList class should hold a pointer to the first and last element of the list (of type MyListElement*) and the MyListElement should hold a MyListElement* next; and MyListElement* prev; pointer, to link the list elements in both directions. In Addition, the MyListElement is supposed to hold data which is to be stored in the List.
So far so good, but now I want the List to be re-usable, meaning I want one List implementation for multiple list-usages, multiple programs. That's why I made the data that's to be stored in the list of type void*, which I have seen done in other C-like languages. That also works, but now my problem: I have seen (and liked) the Java "toString" method which is usually a method that each class has in Java. The Method prints the content/data of the class into a string which is then returned. I wanted to add such a "toString" method to the MyList class, which then iterates through its elements and calls the "toString" method of each element. The MyListElement class should call the toString method of its data, so that if I store data of a third class type in the list, the toString method of that class type is called.
And this is the problem: because the data in MyListElement is of type void*, the compiler throws an error, because it doesn't believe type "void*" has a toString method, which makes sense.

Am I approaching this in the wrong way?
Are there different methods or workarounds?
Thanks...
closed account (D80DSL3A)
In c++ use class templates. This enables any data type to be used in your MyListElements.
Some info here: http://www.cplusplus.com/doc/tutorial/templates/
Thank you, this looks like exactly what I was looking for. It's way more lengthly than I was hoping, but it seems to do the job. I'll try to implement this.

Thanks again.
Topic archived. No new replies allowed.