[lug] typeid calls in g++ 2.95.2

D. Stimits stimits at idcomm.com
Tue Nov 7 15:35:46 MST 2000


"Scott A. Herod" wrote:
> 
> "D. Stimits" wrote:
> 
> > > int whatis( A* a ) {
> >               ^^
> > Here you have specifically told it you are looking for an "A". The
> > argument "a" has been defined as type "A", and although it can hold a
> > "B" (which IS an "A"), it will always be "A". Polymorphism is working
> > correctly here, that it uses the method of "B", but considers it type
> > "A".
> >
> > >     a->spam();
> > >     cout << "We got a " << typeid( a ).name()
> > >          << " while a b* is " << typeid( B* ).name()
> > >          << endl;
> > > }
> 
> Yes, but obviously it is known at runtime what type the object
> labeled "a" truly is so that polymorphism will work correctly.
> 
> It was my understanding that typeid should be able to determine
> that type as well.  That is the reason that typeid only works
> on polymorphic classes.

This is correct...so were the results. Something very subtle is that you
were asking for the typeid of the pointer...not the dereferenced object
that it points TO. Change typeid( a ).name() to become the dereferenced
typeid( *a ).name().

> 
> I need to operate differently on different subclasses of A's and I
> can't edit the classes themselves.  My hideous hack right now is to
> pass the type_info class of b into the whatis() as well and use it.
> 
> Scott
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug




More information about the LUG mailing list