mxjijo
asked on
RTTI : Base class type name v/s Derived
Hello there,
Take a look at the code below
#include <stdio.h>
#include <typeinfo>
class Base {
};
class Drv : public Base {
};
void
printType (char* realName, Base* pbase) {
printf ("\n%s ==> %s\n\n", realName, typeid(pbase).name());
}
int
main(){
printType("base", new Base());
printType("drv", new Drv());
return 0;
}
If you run it (w/msdev) you'll get the following output
base ==> class Base *
drv ==> class Base *
Apparently, the typeinfo contains the info about the local variable of printType() function.
Is it possible to get the type info of the actual object instead ?
Something like this:
base ==> class Base *
drv ==> class Drv *
thanks
~J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great. that would do.
thanks
thanks
Hit the accept button to accept the answer, mxjijo. Otherwise, the question remains open.
ASKER
I'm sorry I thought I did :)
Thanks :-)
Strictly speaking you should use...
#include <cstdio>
...with C++ too.
If you are relatively new to C++, note that typeid and dynamic_cast<> (the two aspects of RTTI) are seldom used and therefore not worth mastering initially. That's why Microsoft doesn't enable RTTI by default. Good OOP design and getting polymorphism to work for you will make them unnecessary 99.999% of the time.