This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: typeid strangeness
Stefan Seefeld wrote:
>
> Ross Smith wrote:
>
> > Did you forget to make _add_ref() virtual? If it's non-virtual, then the
> > this pointer will have the static type of the caller (i.e. always
> > ServantBase) instead of the dynamic type.
>
> no, the method in question is virtual. In fact, I found out that passing
> '*this' instead of 'this' to typeid() works, so I use that now. Still, I
> can't believe the behavior for pointer types is correct.
Aha -- I hadn't noticed that you were calling it on the this pointer
itself rather than *this. The compiler is behaving correctly. See
section 5.2.8 para 3 of the C++ standard. typeid is only polymorphic
when its operand is an lvalue of a polymorphic class type; it doesn't
work with pointers. Arguably this is a bad rule -- it's certainly
inconsistent with dynamic_cast, the other half of the RTTI system -- but
that's what the standard says.
--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
"Unix has always lurked provocatively in the background of the operating
system wars, like the Russian Army." -- Neal Stephenson