Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

RTTI : Base class type name v/s Derived

Posted on 2006-10-24
6
Medium Priority
?
584 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

963 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