 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ulrich Eckhardt Guest
|
Posted: Sat Jan 24, 2004 1:24 pm Post subject: typeid of a pointer and reference |
|
|
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
|
Posted: Mon Jan 26, 2004 10:37 am Post subject: Re: typeid of a pointer and reference |
|
|
"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
). 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 |
|
 |
|
|
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
|
|