Solved

RTTI : Base class type name v/s Derived

Posted on 2006-10-24
6
555 Views
Last Modified: 2012-05-05

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
0
Comment
Question by:mxjijo
  • 4
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 200 total points
ID: 17800423
You need to dereference the pointer because you want the type that's pointed to. You will need a virtual function in the base class too to enable polymorphism. Also compile with /GR to enable RTTI.

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

class Base {
public:
virtual ~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;
}
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17800453
> #include <stdio.h>

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.
0
 
LVL 8

Author Comment

by:mxjijo
ID: 17801505
great. that would do.
thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:rstaveley
ID: 17806700
Hit the accept button to accept the answer, mxjijo. Otherwise, the question remains open.
0
 
LVL 8

Author Comment

by:mxjijo
ID: 17806873

I'm sorry I thought I did :)
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17806948
Thanks :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Starting a function in a c++ application with a TCP command from C# 4 71
FMX enumerated colours 2 84
Problem with SqlConnection 4 160
Issues with C++ Class 19 81
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now