[lug] typeid calls in g++ 2.95.2

Scott A. Herod herod at interact-tv.com
Tue Nov 7 15:02:37 MST 2000


"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.

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




More information about the LUG mailing list