473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How do you find out if object foo is class CBar?

Hi,

I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always
1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then
.....

Can someone tell me the C++ equivelent please?

thanks

George
Jul 22 '05 #1
11 1505
George Styles wrote:
Hi,

I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always
1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then
.....

Can someone tell me the C++ equivelent please?

thanks

George

#include <typeinfo>

// if myObject is of type CBase *, use *myObject
if (typeid(*myObject) == typeid(CSub1))
{
...
}

is the equivalent of IS. If you can resolve this problem by a common
virtual method call, it is better.

Yannick

Jul 22 '05 #2
George Styles wrote:
Hi,

I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its
always 1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then
.....

Can someone tell me the C++ equivelent please?

thanks

George


Try

if ( typeid( myObject ) == typeid ( CSub1* ) )
....
else if ( typeid( myObject ) == typeid ( CSub2* ) )
....

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
Jul 22 '05 #3
Thanks for that :)

George
I would have resolved it using a common virtual
"Yannick Le goc" <le***@imag.fr> wrote in message
news:bu*********@trompette.imag.fr...
George Styles wrote:
Hi,

I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable way) if the object is CSub1 or CSub2. (or neither, but in my app its always 1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then
.....

Can someone tell me the C++ equivelent please?

thanks

George

#include <typeinfo>

// if myObject is of type CBase *, use *myObject
if (typeid(*myObject) == typeid(CSub1))
{
...
}

is the equivalent of IS. If you can resolve this problem by a common
virtual method call, it is better.

Yannick

Jul 22 '05 #4
On Wed, 21 Jan 2004 10:32:39 GMT in comp.lang.c++, "George Styles"
<us****@ripnet.co.uk> was alleged to have written:
Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always
1 or the other)


Don't do that. It's a bad idea. Yes, you can "if(typeid==", but don't.

Instead, you call the appropriate virtual function and the object does
the right thing.

Jul 22 '05 #5
Thanks for your replies...

i went with the virtual function - i dont know why I didnt think of that...
i guess with Delphi IS is so neat (and supported) its better to use IS than
clutter up your objects with loads of extra virtual methods.

George
"George Styles" <us****@ripnet.co.uk> wrote in message
news:bz*********************@news.easynews.com...
Hi,

I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always 1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then
.....

Can someone tell me the C++ equivelent please?

thanks

George

Jul 22 '05 #6

"George Styles" <us****@ripnet.co.uk> wrote in message
news:md*********************@news.easynews.com...
Thanks for your replies...

i went with the virtual function - i dont know why I didnt think of that...


The language provides you with virtual functions so that you do
Object-Oriented-Programming (OOP).
Writing code with if/else to determine the derived class is programming the C
way (Procedural way).
So you know why you chose virtual functions now :-)

Best wishes,
Sharad

Jul 22 '05 #7
I chose virtual functions because in C++ they are far less 'ugly' than the
TYPEINFO thing...

I disagree (in a friendly way...) with you about using IS being
anti-object-oriented though!

But I agree that in C++, virtual functions are the way to go...

Although I ended up with a function that was call IsCBar which returned
false by default, but true in the specific overridden version in CBar... i
dont think this is any more object oriented than using IS

BTW the reason I am doing this is because I have a registerObject function,
which builds up 2 lists, one holding all the objects, and one holding just
the CBar objects (i am writing a keyboard input method for pocketPC and the
CBar special ones are special keys like Shift and Ctrl which stay down when
tapped)

thanks

g

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:bu************@ID-221354.news.uni-berlin.de...

"George Styles" <us****@ripnet.co.uk> wrote in message
news:md*********************@news.easynews.com...
Thanks for your replies...

i went with the virtual function - i dont know why I didnt think of
that...
The language provides you with virtual functions so that you do
Object-Oriented-Programming (OOP).
Writing code with if/else to determine the derived class is programming the C way (Procedural way).
So you know why you chose virtual functions now :-)

Best wishes,
Sharad

Jul 22 '05 #8
George Styles wrote:
I chose virtual functions because in C++ they are far less 'ugly' than the
TYPEINFO thing...

I disagree (in a friendly way...) with you about using IS being
anti-object-oriented though!


1. Please don't top post.

Now, as to IS...

You have a hierarchy {class bodies redacted}:

class base { };
class d1 : public base { };
class d2 : public base { };
You have tons of code scattered around that say:

if anObject IS base
anObject.do_base();
else if anObject IS d1
anObject.do_d1();
else if anObject IS d2
anObject.do_d2();

Great.

Now what happens when you add

class d3 : public base { }; ???

You have to go through and find and modify ALL those if statements. The
client code has to have knowledge of your class hierarchy. Whereas if
you use virtual functions (and I believe Delphi has the OVERRIDE keyword
to do the same thing), then you have { class bodies and public/privates
redacted } :

class base: { virtual void do_thing(); };
class d1 : public base { void do_thing(); };
class d2 : public base { void do_thing(); };

And your client code now becomes

anObject.do_thing();

(Note to gurus, you can assume anObject is a base&).

Now, if you add d3

class d3 : public base { void do_thing(); };

Then your client code requires ZERO changes. It is more "object
oriented" because you have an object, and you tell it what to do
(do_thing), and it "knows" how to do the right thing.

I know that the gurus here could probably describe it better....
Jul 22 '05 #9
> I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always
1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then


if (dynamic_cast<CSub1 *>(myPtr)) { /* ... */ }

I renamed your variable - I filed calling a pointer "myObject" confusing.

NOTE: this will succeed if myPtr points to a CSub1, or any class
derived from CSub1. If it has to be exactly a CSub1 then you will
have to use the typeid() suggested by other posters.
Jul 22 '05 #10

"George Styles" <us****@ripnet.co.uk> wrote in message
news:nH*********************@news.easynews.com...
I chose virtual functions because in C++ they are far less 'ugly' than the
TYPEINFO thing...

I disagree (in a friendly way...) with you about using IS being
anti-object-oriented though!

But I agree that in C++, virtual functions are the way to go...
Good..you agree somewhere with me :-)
Although I ended up with a function that was call IsCBar which returned
false by default, but true in the specific overridden version in CBar... i
dont think this is any more object oriented than using IS

BTW the reason I am doing this is because I have a registerObject function,
which builds up 2 lists, one holding all the objects, and one holding just
the CBar objects (i am writing a keyboard input method for pocketPC and the
CBar special ones are special keys like Shift and Ctrl which stay down when
tapped)


As already posted by red floyd, the reason to use virtual functions is so that
your old code calls your new code.
Using if/else as he has pointed out means that you need to modify your code as
and when new classes get introduced.

Let me quote Scott Meyers now (hopefully you will agree with a guru like Scott)
<Quote from EC++>
Anytime you find yourself writing code of the form, "if the object is of type
T1, then do something, but if it's of type T2, then do something else," slap
yourself. That isn't The C++ Way. Yes, it's a reasonable strategy in C, in
Pascal, even in Smalltalk, but not in C++. In C++, you use virtual functions.
</Quote from EC++>

I am referring to Item No. 39 from his book "Effective C++".
btw, it's a very informative + humorous book..pls. don't take the "slap thing"
in a derogatory manner :-)

Best wishes,
Sharad


Jul 22 '05 #11
In terms of performance, which is faster:

if (dynamic_cast<CSub1*>(myPtr)) { /* ... */ }

or:

if (typeid(myPtr) == typeid(CSub1*)) { /* ... */ }

?

Jamie Burns.

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om...
I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.

Given that I have a pointer to CBase, can I find out (easily in a portable way) if the object is CSub1 or CSub2. (or neither, but in my app its always 1 or the other)

In Delphi, you would use the IS operation, eg

if myObject IS CSub1 then


if (dynamic_cast<CSub1 *>(myPtr)) { /* ... */ }

I renamed your variable - I filed calling a pointer "myObject" confusing.

NOTE: this will succeed if myPtr points to a CSub1, or any class
derived from CSub1. If it has to be exactly a CSub1 then you will
have to use the typeid() suggested by other posters.

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
6
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following....
3
by: Poewood | last post by:
Okay here are four classes for a pocket pc program: Input, fpositional, ComboBoxArray and TextBoxArray. The "input" class is the form. I use the fpositional class to handle most of the functions...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
5
by: Joerg Battermann | last post by:
Hello there, I have a custom type defined via Public Class Requirement Public IDNumber As Integer Public Name As String Public Description As String Public VersionPlanAttributes As New _
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
5
by: jm.suresh | last post by:
Hi I have three objects, all of them are instances of classes derived from a base class. Now, given one of the instance, I want to find the closest relative of the other two. How can I do this? ...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
10
by: CodeNoob | last post by:
please help been working on a project got it down to 5 errors from 100 now i have no idea what to do. Errors: init: deps-jar: Created dir: C:\Users\Tommy\Desktop\build\classes Compiling 306...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.