C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how can i know weather Object A is an instance of class B.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
lfeiman888@gmail.com
Guest





PostPosted: Mon Feb 27, 2006 11:06 am    Post subject: how can i know weather Object A is an instance of class B. Reply with quote



Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Kodt
Guest





PostPosted: Mon Feb 27, 2006 3:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote



lfeiman888 (AT) gmail (DOT) com wrote:
Quote:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

If the class B has virtual functions, and RTTI is on (see options of
your compiler; for VC it is /GR), then use

1) dynamic_cast

if( dynamic_cast<B*>(pA) != NULL ) cout << "Yes, it is B (or its
descendant)";

Note that dynamic_cast failure on pointers returns NULL but on
references throws std::bad_cast.

2) typeid

if( typeid(*pA) == typeid(B) ) cout << "Yes, it is exactly B";

-------------
And if you need to check it in compile time, use
boost::is_base_and_derived<B,A>


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Eugene Alterman
Guest





PostPosted: Mon Feb 27, 2006 3:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote



<lfeiman888 (AT) gmail (DOT) com> wrote in message
news:1141032114.073823.136700 (AT) i40g2000cwc (DOT) googlegroups.com...
Quote:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

If you have a pointer or a reference to an object of a polymorphic type (a
class that has at least one virtual member function) you can use
dynamic_cast:

class B
{
public:
....
virtual ~D();
....
};

class D: public B
{
....
};

void foo(B* pb)
{
D* pd = dynamic_cast<D*>(pb);
if(pd != 0) // pb points to an instance of D (or a class
publicly derived from D)
....
};

Note: if you use Microsoft VC make sure you have "Enabled Run Time Type
Info" flag (/GR) on (it is off by default).


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Feb 27, 2006 3:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

* lfeiman888 (AT) gmail (DOT) com:
Quote:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

I'm surprised the moderators let this posting through; perhaps there's
mainly one very overworked moderator doing the screening now?

First, the question is platform specific, not concerning standard C++,
and it's platform specific in the way which caused clc++m to be created.

And second, it smells like a homework problem or just trolling (the
names mentioned do indeed appear in that library, if memory serves me
right).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dietmar Kuehl
Guest





PostPosted: Mon Feb 27, 2006 3:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

lfeiman888 (AT) gmail (DOT) com wrote:
Quote:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

You can test the result of 'dynamic_cast<>()':

if (B* b_ptr = dynamic_cast<B*>(&A))
std::cout << "the object 'A' is of class 'B'\n";

assuming 'A' refers to a reference to the object. If 'A' is actually
a pointer to the object, you would, of course, just omit taking the
address (i.e. drop the '&').
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
benben
Guest





PostPosted: Mon Feb 27, 2006 3:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

lfeiman888 (AT) gmail (DOT) com wrote:
Quote:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

Well if the object is of a polymorphic class then the answer is yes,
just use dynamic_cast.

However if the object is of a non-polymorphic type then you will have to
do some round about way...take a look at boost::any

Regards,
Ben

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Eugene Alterman
Guest





PostPosted: Mon Feb 27, 2006 5:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote in message
news:46gc08FauneeU1 (AT) individual (DOT) net...
Quote:
* lfeiman888 (AT) gmail (DOT) com:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

I'm surprised the moderators let this posting through; perhaps there's
mainly one very overworked moderator doing the screening now?

First, the question is platform specific, not concerning standard C++,
and it's platform specific in the way which caused clc++m to be created.

And second, it smells like a homework problem or just trolling (the
names mentioned do indeed appear in that library, if memory serves me
right).

The confusion seems to have been caused by the ambiguous parsing of the
question :-)

The OP meant:
"Is there some methods (in the standard C++) like <IsKindOf
,RUNTIME_CLASS in MFC>?" ,
and not:
"Is there some methods like <IsKindOf ,RUNTIME_CLASS> in MFC?"

MFC provides its own RTTI implementation, and many MFC programmers are not
used to the standard C++ RTTI facilities.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Martin Bonner
Guest





PostPosted: Mon Feb 27, 2006 5:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

Alf P. Steinbach wrote:
Quote:
* lfeiman888 (AT) gmail (DOT) com:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

I'm surprised the moderators let this posting through; perhaps there's
mainly one very overworked moderator doing the screening now?

First, the question is platform specific, not concerning standard C++,
and it's platform specific in the way which caused clc++m to be created.

There is nothing platform specific about the question "How can I know
whether Object A is an instance of class B".

I suspect that you have been confused because the main question is only
in the title of the post, and the body of the post just contains
supplementary information (which is poor netiquette, but not bad enough
to provoke rejection).

Quote:
And second, it smells like a homework problem or just trolling (the
names mentioned do indeed appear in that library, if memory serves me
right).

I think a question of the form "is there a platform independant way to
achieve this functionality (the equivalent of this platform specific
code)" is entirely on-topic. I don't think it is homework, because
IsKindOf is a moderately advanced MFC feature - not one I would expect
students to be introduced to.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Mon Feb 27, 2006 5:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

<lfeiman888 (AT) gmail (DOT) com> wrote in message
news:1141032114.073823.136700 (AT) i40g2000cwc (DOT) googlegroups.com...
: Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
: thanks to all that helps.

Look for dynamic_cast and typeid/type_info in your favorite
C++ reference (or web search engine).


Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alex Beluga
Guest





PostPosted: Mon Feb 27, 2006 5:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

lfeiman888 (AT) gmail (DOT) com wrote:
Quote:
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

If I quite right understood you, you have to throw object you want to
check and try to catch it as a class you want to check it for.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
john
Guest





PostPosted: Mon Feb 27, 2006 9:06 pm    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

Eugene Alterman wrote:
Quote:
lfeiman888 (AT) gmail (DOT) com> wrote in message
news:1141032114.073823.136700 (AT) i40g2000cwc (DOT) googlegroups.com...
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

If you have a pointer or a reference to an object of a polymorphic type (a
class that has at least one virtual member function) you can use
dynamic_cast:

Could someone explain why the class has to be polymorphic for

dynamic_cast to work ?

Thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Tue Feb 28, 2006 9:06 am    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

"john" <john.tylo (AT) nospam (DOT) teleme.com> wrote in message
news:44033aa0$0$20139$8fcfb975 (AT) news (DOT) wanadoo.fr...
: Eugene Alterman wrote:
: > <lfeiman888 (AT) gmail (DOT) com> wrote in message
: > news:1141032114.073823.136700 (AT) i40g2000cwc (DOT) googlegroups.com...
: >> Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
: >> thanks to all that helps.
: >
: > If you have a pointer or a reference to an object of a polymorphic
type (a
: > class that has at least one virtual member function) you can use
: > dynamic_cast:
: >
: Could someone explain why the class has to be polymorphic for
: dynamic_cast to work ?

If type identification is to be performed for an object at
run time, some information needs to be associated with the
object - which implies some overhead. The default language
design choice in C++ is "you don't pay for what you don't
use", so this overhead is not present by default.

This additional type information actually comes for free
if the object already contains a virtual function pointer
to support polymorphic calls. And polymorphic objects
for which run-time type identification is of interest
typically need to have a virtual destructor anyway.
So it simply makes sense to combine these two features.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Markus Moll
Guest





PostPosted: Tue Feb 28, 2006 9:06 am    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

Hi

john wrote:

Quote:
If you have a pointer or a reference to an object of a polymorphic type
(a class that has at least one virtual member function) you can use
dynamic_cast:

Could someone explain why the class has to be polymorphic for
dynamic_cast to work ?

Most probably because the virtual table is used for type identification.
If you only have

struct Base
{
int x;
};

struct Derived : public Base
{
void f() { x = 5; }
};

the layout of Derived/Base will probably just be "one int". Given a pointer
to Base, how would you find out if the object stored at that location has
some other dynamic type?

C++ was designed so that you need not pay for features that you do not use.
This means that in the simple scenario shown above there won't be any extra
information from which the dynamic type could be determined.

On the other hand, if you have virtual functions, the dynamic type of an
object must be determined at runtime anyway.

Markus


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob Bell
Guest





PostPosted: Tue Feb 28, 2006 9:06 am    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

john wrote:
Quote:
Eugene Alterman wrote:
lfeiman888 (AT) gmail (DOT) com> wrote in message
news:1141032114.073823.136700 (AT) i40g2000cwc (DOT) googlegroups.com...
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

If you have a pointer or a reference to an object of a polymorphic type (a
class that has at least one virtual member function) you can use
dynamic_cast:

Could someone explain why the class has to be polymorphic for
dynamic_cast to work ?

The short answer is "because the standard says so." I imaging a longer
answer would be something along the lines of "otherwise there's no
reasonable way to implement it." A typical implementation of virtual
functions puts a pointer to a data structure containing function
pointers into each polymorphic object; to make RTTI work, an
implementation would put RTTI data into the structure as well. If there
are no virtual functions, there's no virtual function data structure
and no RTTI data.

Bob


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
fei.liu@noaa.gov
Guest





PostPosted: Tue Feb 28, 2006 9:06 am    Post subject: Re: how can i know weather Object A is an instance of class Reply with quote

john wrote:
Quote:
Eugene Alterman wrote:
lfeiman888 (AT) gmail (DOT) com> wrote in message
news:1141032114.073823.136700 (AT) i40g2000cwc (DOT) googlegroups.com...
Is there some methods like IsKindOf ,RUNTIME_CLASS in MFC.
thanks to all that helps.

If you have a pointer or a reference to an object of a polymorphic type (a
class that has at least one virtual member function) you can use
dynamic_cast:

Could someone explain why the class has to be polymorphic for
dynamic_cast to work ?

Thanks.

The standard requires a polymorphic object to contain RTII. It's
impossible to apply dynamic_cast on an object without RTII.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.