This function does not work correctly for multiple inheritance, but ONLY for single inheritance. In addition, the type number which is returned is NOT unique across program executions.
This RTTI mechanism is simple, it does not require extra functions to be added to the classes which are to be identified, and it is similar to the RTTI mechanism which will be a part of the C++ language in the future.
To be useful, however, this mechanism must be used as part of the implementation of a virtual member function. For example:
#include <casa/Utilities/Register.h> #include <iostream> class foo { public: virtual uInt type() { return Register(this);}}; class bar : public foo { public: uInt type() { return Register(this);}}; main() { foo *b = new bar(); foo *f = new foo(); cout << "f: type()=" << f->type() << " Register()=" << Register(f) << endl; cout << "b: type()=" << b->type() << " Register()=" << Register(b) << endl; }The output of this code would look something like:
f: type()=1 Register()=1 b: type()=2 Register()=1Without the virtual function, type(), the output of Register() is deceiving and of little use.
This is a templated function which takes a pointer to any class as a parameter. The parameter's type is then used to generate a unique id. The parameter is a pointer rather than a value for efficiency reasons. With a value parameter, it would be difficult to do things like:
Register(static_cast<MyClass*>(0));to find the Register type id for a random class.