473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to find out object type - PLEASE HELP

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.
Jul 22 '05 #1
8 1552
"Gandu" <cp**********@yahoo.com> wrote in message
news:3c**************************@posting.google.c om...
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?
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #2
On 17 Apr 2004 11:06:41 -0700 in comp.lang.c++, cp**********@yahoo.com
(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;

Jul 22 '05 #3

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.


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

Jul 22 '05 #4
* "Mark A. Gibbs" <x_*********@rogers.com_x> schriebt:

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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5

Alf P. Steinbach wrote:
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

Jul 22 '05 #6
cp**********@yahoo.com (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.


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...
Jul 22 '05 #7
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.
"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message news:<PI********************@twister01.bloor.is.ne t.cable.rogers.com>...
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.


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

Jul 22 '05 #8

Gandu wrote:
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

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Christopher J. Bottaro | last post by:
I actually want all the parent classes too. So if D derives off C derives off B derives off A, I ultimately want a tuple ('D', 'C', 'B', 'A'). For those of you following the Python Documentation...
5
by: Tom | last post by:
How can I find the first blank control on a form by looking at the controls in the order of their tab order? Thanks! Tom
2
by: Xiangliang Meng | last post by:
Hi, all. As far as I know, the speical arithmetic operator on void pointer is an externsion in GNU CC. However, I could not find the relative topics in C99. Would someone like to help me find...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
by: sck10 | last post by:
Hello, I am using the code below to set the values of a DetailsView template field using FindControl. My question is how would you find a control if its a Boundfield control? For example,...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: winston.heng | last post by:
Hi, Thanks for reading this posting. I have been cracking my head on solving the infinite loop when i call the following section code. Any help or advise is greatly appreciated =D Thanks in...
18
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.