Solved

RTTI : Base class type name v/s Derived

Posted on 2006-10-24
6
582 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

634 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