Topic: RTTI in g++


Author: mrs@cygnus.com (Mike Stump)
Date: Fri, 4 Nov 1994 03:02:01 GMT
Raw View
Below is a real simple program that will run with g++, that shows
basically what RTTI is all about.

When run, you'll notice that it knows ptr points to a B object...

Now, you may thing this is done with smoke and mirrors, as ptr is
after all assigned just above...  Nope, it really does work, the right
way.  :-)

Soon Exception Handling will make full use of the RTTI support, and
you'll be able to catch thrown objects, based upon their dymanic type.

:-)

Does your C++ compiler have RTTI?  Extended RTTI?  Maybe it's time to
switch...

Note, to get this cutting edge feature now, you will have to get
ftp.cygnus.com:pub/rotd.tar.gz, as no other g++ compiler yet has RTTI
in it.  :-) You can find typeinfo.{cc,h} in typeinfo.shar in the same
place.

Just thought I should whet your appetite.


#include <stdio.h>
#include <typeinfo.h>

class A {
  virtual foo() { }
} a, *ptr;

class B : public A {
} b;

main() {
  ptr = &b;
  printf("The name is %s\n", typeid(*ptr).name());
}