Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Itanium-based Systems: HP aC++/HP C Programmer's Guide > Chapter 6 Standardizing Your Code

type_info Class

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

type_info is a class in the standard header file <typeinfo>. A reference to an instance of this class is returned by the typeid operation.

Implementations may differ in the exact details of this class, but in all cases it is a polymorphic type (has virtual functions) that allows comparisons and a way to access the name of the type.

Usage:

This class is useful for diagnostic information and for implementing services on objects where it is necessary to know the exact type of the object.

Example:

# include <iostream.h>
# include <typeinfo>

class Base {
virtual void f(); // Must have a virtual
// function to be a
// polymorphic type

// additional class details omitted

};

class Derived : public Base {
// class details omitted
};

void Base::f()
{
// Define function from Base.
}

void main ()
{
Base *p;

// code which does either
// p = new Base; or
// p = new Derived;

if (typeid(*p) == typeid(Base)) // Standard requires
// comparison as part
// of this class.
cout << “Base Object\n”;

cout << typeid(*p).name() << ‘\n’; // Standard requires
// access to the name
// of the type.
}

The standard requires the class type_info to be polymorphic. You cannot assign or copy instances of the class (the copy constructor and assignment operators are private). The interface must include:

int operator == (const type_info&) const

int operator !=( const type_info&) const

const char * name() const

int before (const type_info&) const

The operators allow comparison of object types. The name() function allows access to the character string representing the name of the object. The before() function allows types to be sorted. This allows them to be accessed through hash tables. The before() function is not a lexical ordering; it might not yield the same results. The name() function now returns the mangled name of a type as per the C++ ABI. This string can not be demangled with __cxa_demangle.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.