0
class Security {
public:
	virtual ~Security() {}
};

class Stock: public Security {};
class Bond: public Security {};

class Investment: public Security {
public:
	void special()
	{
		cout << "Special Investment function" << endl;
	}
};

class Metal: public Investment {};

int main()
{
	Security* s = new Metal;
	cout << typeid(s).name() << endl;
	cout << typeid(*s).name() << endl;
}

OUTPUT:

P8Security
5Metal

Why it always introduces in front of the name,numbers ?

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by vijayan121
0

Must be a compiler-specific thing. When I run this on VC++ 2008 I get:

class Security *
class Metal

Edited by Fbody: n/a

0

> Why it always introduces in front of the name,numbers?

std::type_info::name returns an implementation defined C-style string.

For the compiler that you are using (GCC 3.x or above), you can demangle with abi::__cxa_demangle()

#include <typeinfo>
#include <string>
#include <cxxabi.h>
#include <stdexcept>
#include <cstdlib>

std::string demangled_name( const std::type_info& tinfo )
{
    int status = 0 ;
    std::size_t length = 0 ;
    char* temp = abi::__cxa_demangle( tinfo.name(), 0, &length, &status ) ;
    if( status != 0 ) throw std::runtime_error( "demangle error" ) ;
    std::string name(temp) ;
    std::free(temp) ;
    return name ;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.