Solved

Differentiate objects in a list

Posted on 2003-10-29
5
188 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now