How to find out object type - PLEASE HELP

G

Gandu

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.
 
J

Jakob Bieling

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.

Maybe the typeid operator is of help?
 
D

David Harmon

On 17 Apr 2004 11:06:41 -0700 in comp.lang.c++, (e-mail address removed)
(Gandu) wrote,
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.

If your pointer is void* that means you do not care about what type the
object is, so the question is irrelevant. Cast not thy pointers into
the void.

If you are using a template on type T, there are several techniques,
depending on just what you mean by "find out the type." What are you
trying to accomplish? In general, it is probably better to write code
where you do not do a lot of "finding out" of types.

For example:

typedef std::list<myclass> mylist;
mylist some_list;
mylist::value_type some_variable;
 
M

Mark A. Gibbs

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
 
A

Alf P. Steinbach

* "Mark A. Gibbs said:
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

Have never tried that in C++, but what about e.g.


std::list<boost::any> theList;


assuming that works, isn't that easy?
 
M

Mark A. Gibbs

Alf said:
std::list<boost::any> theList;


assuming that works, isn't that easy?

Yes and no. First of all, Boost.Any does not hold *anything*. But if you
are ok with the limitations on what it can hold, you're free to use it.

Secondly, I thought I made it clear I was referring doing it yourself,
but I can see that I didn't. Duplicating the capabilities of Boost.Any
is no trivial task.

mark
 
D

Daniel T.

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.

How could you not know what the type of the contained objects are? You
created the list!

std::list<std::string> string_list;

I know the objects in the string_list are of type std::string. Maybe
there is some miscommunication here...
 
G

Gandu

Thank you all for your very useful suggestions. A colleague had suggested a
generic container class be created that will hold "anything", so this question
came up.
 
M

Mark A. Gibbs

Gandu said:
Thank you all for your very useful suggestions. A colleague had suggested a
generic container class be created that will hold "anything", so this question
came up.

If I were you, I'd take your colleague to task on why this is necessary.
I can't see a reasonable argument in favour of doing that. I mean, what
possible use can there be for holding an integer, a string, a stream, a
student an a* path analyser with tactical weighting and a random number
generator in the same place?

mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top