Gandu said:
Could some C++ guru please help me? Suppose I have a linked list, in which the
data stored is of type T (using templates) or void* (as in C). Is there any
way to find out the type of the object (other than type-casting) when the
object is extracted fronm the list? Thanks in advance for your help.
Don't use void*.
If you define your linked list class like:
template <typenameT>
class LinkedList
{
public:
typedef element_type T;
// etc.
};
Then you can determine the element type like this:
template <typename T>
void foo(LinkedList<T> const&)
{
cout << typeid(LinkedList<T>::element_type).name();
}
But I not sure that's what you are trying to do. Are you trying to make
a list that can hold *anything*? That's not easy (and generally not
wise). Or are you trying to make a list that can hold any object derived
from a specific base class? That's easier. All you would have to do in
that case is use typeid() on the extracted objects.
It would help if you gave more information on what you are trying to do
and why. It is not common to need to get the type of an object. There is
probably a better solution that you're not seeing.
mark