Solved

Differentiate objects in a list

Posted on 2003-10-29
5
207 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
5 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 25 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 25 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

832 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