Solved

Differentiate objects in a list

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 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