[lug] typeid calls in g++ 2.95.2

Scott A. Herod herod at interact-tv.com
Tue Nov 7 14:05:30 MST 2000


Sorry to post a C++ question but it is rather specific to the egcs
compilers.

Does anyone know if typeid works correctly on the egcs compilers, or 
perhaps I am not using it correctly.  In the code posted just below,
I would expect the output to be:

------------------------------
This is a B
We got a P1B while a b* is P1B
------------------------------

but I get

------------------------------
This is a B
We got a P1A while a b* is P1B
------------------------------

The code is just below.  Compile with "g++ filename.C".
Thanks,
Scott

--------------------------------
#include <typeinfo>
#include <stream.h>

class A {
public:
    A() {};
    virtual ~A() {};
    virtual void spam() { cout << "This is an A" << endl; }
};

class B : public A {
public:
    B() {};
    virtual ~B() {};
    virtual void spam() { cout << "This is a B" << endl; }
};

int whatis( A* a ) {
    a->spam();
    cout << "We got a " << typeid( a ).name()
	 << " while a b* is " << typeid( B* ).name()
	 << endl;
}

int main ( int argc, char *argv[] ) {
    B* b = new B;
    whatis( b );
    return 0;
}
-------------------------------------------




More information about the LUG mailing list