Solved

Differentiate objects in a list

Posted on 2003-10-29
5
233 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

623 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