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 

typeid of a pointer and reference

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ulrich Eckhardt
Guest





PostPosted: Sat Jan 24, 2004 1:24 pm    Post subject: typeid of a pointer and reference Reply with quote



Greetings!

I stumbled arcoss some code that puzzled me. The first snippet uses pointers
like this:

Base* p1 = new Base;
Base* p2 = new Derived;
assert( typeid(p1) == typeid(p2) );

I assume this assertion will never fire.

Base& r1 = aBase;
Base& r2 = aDerived;
assert( typeid(r1) == typeid(r2) );

I assume that this assertion will fire, but I wonder if it always will or
only when class Base has virtual parts, so it can determine the dynamic
type from the reference.

thanks

Uli


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





PostPosted: Mon Jan 26, 2004 10:37 am    Post subject: Re: typeid of a pointer and reference Reply with quote



"Ulrich Eckhardt" <doomster (AT) knuut (DOT) de> wrote

Quote:
Greetings!

I stumbled arcoss some code that puzzled me. The first snippet uses
pointers
like this:

Base* p1 = new Base;
Base* p2 = new Derived;
assert( typeid(p1) == typeid(p2) );

I assume this assertion will never fire.

Correct, since pointers are not polymorphic types.

Quote:

Base& r1 = aBase;
Base& r2 = aDerived;
assert( typeid(r1) == typeid(r2) );

I assume that this assertion will fire, but I wonder if it always
will or
only when class Base has virtual parts, so it can determine the
dynamic
type from the reference.


If Base is a polymorphic type, i.e., if it is a class type defining
one or more virtual functions, the assertion will fire (in debug mode
Smile ). Otherwise, both typeid expressions refer to type_info objects
representing the type Base. The relevant section of the standard is
5.2.8, paragraphs 2 and 3.






[ 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
Page 1 of 1

 
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.