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 ?

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

class Security *
class Metal

> 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 ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.