Solved

RTTI : Base class type name v/s Derived

Posted on 2006-10-24
6
545 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
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.

744 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

13 Experts available now in Live!

Get 1:1 Help Now