RTTI Strings

There’s not much to this, but so that there’s a simple example here:

const char * type_str = typeid (expression).name();

This causes a (compiler-specific) string representing the type of <expression> to be assigned to <type_str>. In the case of MSVC, this is quite a readable representation of the type, but I don’t think the C++ standard imposes much on this other than uniqueness between different types.

The <type_info> structure must have been declared. Thus:

#include <typeinfo>

Of course, RTTI must be enabled for the build. In Visual Studio, this option may be found in the project properties under C/C++ | Language.


One Response to “RTTI Strings”

  1. Rob Says:

    2010-07-27: updated with include and property setting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: