Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

RTTI : Base class type name v/s Derived

Posted on 2006-10-24
6
568 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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