Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Differentiate objects in a list

Posted on 2003-10-29
5
Medium Priority
?
237 Views
Last Modified: 2010-04-02
Once I had added an object to a list or vector, how do I determine whether the object is an instance of class A or class B?

Thank you.
0
Comment
Question by:kimchg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 75 total points
ID: 9642428
Don't add the object, add a pointer to it

Say,

class TBase;

clasee TA : public TBase;

class TB : public TBase;

vector<TBase*> MyVec

then, to check

TBase *lBase = MyVec[n];

TA *lA = dynamic_cast<TA*>(lBase);

i f( lA )
{
  it's a TA
}

else

... similar dynamic cast for the B
0
 
LVL 5

Assisted Solution

by:drnick
drnick earned 75 total points
ID: 9642494
i don't know what you want to do exactly, but, however, i assume something.

there are two common ways to determine the class type of an object

1. include rtti (runtime type information) into your c++ proggi.
    (therefore, you need to check it in project/setting..)
    then you heave the typeid and type_info operators.
    microsoft has written something about it at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_run.2d.time_type_information.asp

2. let all the objects you store (or better their classes) derive from one base class which has a virtual routine like
     virtual int my_type(void);
    override it in any class you'd like to store and give back another int.
    then you can compare the return values of the routines two objects in the list, for example
    this can be faster and smaller than including rtti, depends on what you want

if you have additional need of information, just tell
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9642513
Alternative solution: If A and B derive from one class you can have a ClassName function that returns the name of the class. Override the function in each subclass. Doesn't depend on RTTI.

Of course if you find yourself needing to do this (identify the actual type in a polymorphic container), you should look carefully at your design. Why do you need to know? I would guess that a virtual function could remove the need to know the actual type.

-bcl
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546287
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: andrewjb {http:#9642428} & drnick {http:#9642494}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question