1,typeid操作符

用法和sizeof很像,既可以用于变量,也可以用于类型,作用是指出他们的类型。

#include<iostream>
#include <c++/memory>
using namespace std;

class A
{

};

int main()
{
cout<<typeid(12).name()<<" ";
cout<<typeid(int).name()<<" ";
cout<<typeid(1.0).name()<<" ";
cout<<typeid(double).name()<<" ";
cout<<typeid('c').name()<<" ";
cout<<typeid(char).name()<<" ";
cout<<typeid("s").name()<<" ";
cout<<typeid(string).name()<<" ";
cout<<endl;
cout<<typeid(int*).name()<<" ";
cout<<typeid(char*).name()<<" ";
cout<<typeid(double*).name()<<" ";
cout<<endl;
cout<<typeid(A).name()<<" ";
cout<<typeid(A*).name()<<" ";
return 0;
}

输出:

i i d d c c A2_c NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

Pi Pc Pd

1A P1A


2,RTTI

RTTI即Run-Time Type Identification,通过运行时类型信息程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。

#include<iostream>
#include <c++/memory>
using namespace std;

class C
{
public:
virtual string toString()
{
return "class C";
}
};
class B:public C
{
public:
string toString()
{
return "class B";
}
};
class A :public B
{
public:
string toString()
{
return "class A";
}
};

void display(C *p)
{
cout << p->toString() << " "<<typeid(*p).name()<<endl;
}

int main()
{
C* p= new A();
display(p);
p= new B();
display(p);
p= new C();
display(p);
return 0;
}

输出:

class A 1A

class B 1B

class C 1C


3,type_info

type_info类的作用就是提供typeid关键字

class type_info
{
public:
/** Destructor first. Being the first non-inline virtual function, this
* controls in which translation unit the vtable is emitted. The
* compiler makes use of that information to know where to emit
* the runtime-mandated type_info structures in the new-abi. */
virtual ~type_info();

/** Returns an @e implementation-defined byte string; this is not
* portable between compilers! */
const char* name() const _GLIBCXX_NOEXCEPT
{ return __name[0] == '*' ? __name + 1 : __name; }

#if !__GXX_TYPEINFO_EQUALITY_INLINE
// In old abi, or when weak symbols are not supported, there can
// be multiple instances of a type_info object for one
// type. Uniqueness must use the _name value, not object address.
bool before(const type_info& __arg) const _GLIBCXX_NOEXCEPT;
bool operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT;
#else
#if !__GXX_MERGED_TYPEINFO_NAMES
/** Returns true if @c *this precedes @c __arg in the implementation's
* collation order. */
// Even with the new abi, on systems that support dlopen
// we can run into cases where type_info names aren't merged,
// so we still need to do string comparison.
bool before(const type_info& __arg) const _GLIBCXX_NOEXCEPT
{ return (__name[0] == '*' && __arg.__name[0] == '*')
? __name < __arg.__name
: __builtin_strcmp (__name, __arg.__name) < 0; }

bool operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT
{
return ((__name == __arg.__name)
|| (__name[0] != '*' &&
__builtin_strcmp (__name, __arg.__name) == 0));
}
#else
// On some targets we can rely on type_info's NTBS being unique,
// and therefore address comparisons are sufficient.
bool before(const type_info& __arg) const _GLIBCXX_NOEXCEPT
{ return __name < __arg.__name; }

bool operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT
{ return __name == __arg.__name; }
#endif
#endif
bool operator!=(const type_info& __arg) const _GLIBCXX_NOEXCEPT
{ return !operator==(__arg); }

#if __cplusplus >= 201103L
size_t hash_code() const noexcept
{
# if !__GXX_MERGED_TYPEINFO_NAMES
return _Hash_bytes(name(), __builtin_strlen(name()),
static_cast<size_t>(0xc70f6907UL));
# else
return reinterpret_cast<size_t>(__name);
# endif
}
#endif // C++11

// Return true if this is a pointer type of some kind
virtual bool __is_pointer_p() const;

// Return true if this is a function type
virtual bool __is_function_p() const;

// Try and catch a thrown type. Store an adjusted pointer to the
// caught type in THR_OBJ. If THR_TYPE is not a pointer type, then
// THR_OBJ points to the thrown object. If THR_TYPE is a pointer
// type, then THR_OBJ is the pointer itself. OUTER indicates the
// number of outer pointers, and whether they were const
// qualified.
virtual bool __do_catch(const type_info *__thr_type, void **__thr_obj,
unsigned __outer) const;

// Internally used during catch matching
virtual bool __do_upcast(const __cxxabiv1::__class_type_info *__target,
void **__obj_ptr) const;

protected:
const char *__name;

explicit type_info(const char *__n): __name(__n) { }

private:
/// Assigning type_info is not supported.
type_info& operator=(const type_info&);
type_info(const type_info&);
};