- 浏览: 389939 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
typeid is the one of the meager offering of runtime type information hierarchy functionality. what it offers is the ability to determine at runtime the real type of an object (a pointer is a special type of variable, which we will lsee shorty in this post )
what is the typeid operator used for?
it is used in advanced programming developement, when building debugger for example, or it is used to deal with persistent object through a pointer or a reference to a base class.
The program needs to find out the actual type of the object manipulated to list the properties of an object properly during a debugging session or to store and retrieve an object property
During a debugging session or to store and retrieve an object property properly to find out the actual type of an object, the typeid operator can be used.
For better describe what we will describe, here is the class hiearchy that we will use in this example.
class employee { }; class manager : public employee {}; class programmer : public employee { };
so, in the simplest case, what is the output of the followiong.
void typeid_test() { programmer obj ; programmer &re = obj; // we look at name() in the subsection on type_info // below, name() returns the C-style string: "programmer" cout << typeid(re).name() << endl; }
the typeid can accept two types of operand, one is an expression and the other is the name of the type. Let's first see the first type of operand (the later case will be covered in example later) .
/** * the operand of the typeid operator can be expression or type names of any type * */ void typeid_operand() { int iobj; cout << typeid(iobj).name() << endl; // print : ints cout <<typeid(8.16).name() << endl; // prints : double }
when the oeprand to typeid operator is a class, but not a class of virtual function, the result of the typeid operator is well, not the type of underlying object.
/** * if the typeid operand is a class type, but not of a class type with virtual functions. then the typeid operator indicates the type of * the operand is well, not the type of the underlying object */ class Base { }; class Derived : public Base { }; void typeid_non_virtual_class() { Derived dobj; Base *pb = &dobj; cout << typeid(*pb).name() << endl; // prints : Base }
but, if the class does have virtual function, then the real derived object will be retrieved and its true type will be returned.
/* * if the typeid oeprand is a class type , where if class to be examined is of virutal functions, then the * real derived classes will be returned. */ void typeid_result_virtual_class() { employee *pe = new manager(); employee &re = *pe; if (typeid(*pe) == typeid(manager)) cout << "typeid(*pe) == typeid(manager)" << endl; // true if (typeid(re) == typeid(employee)) cout << "typeid(re) == typeid(employee)" << endl; // true if (typeid(&re) == typeid(employee *)) cout << "typeid(&re) == typeid(employee *)" << endl; // true if (typeid(&re) == typeid(manager *)) cout << "typeid(&re) == typeid(manager *)" << endl; // false }
you may have seen already that there are some difference between the object and poiner, the pointer type itself is fixed, so to get the dynamic behavior of typeid, you'd better pass in the real object or reference (which will be treated as the data object)
type_info class
You may find the type_info class declaration as follow. actually the real implementation can vary from implemenation to implemenation.
class type_info { private: type_info(const type_info&); type_info& operator = (const type_info &); public: virtual ~type_info(); int operator ==(const type_info&) const; int operator!=(const type_info &) const; const char *name() const; };
the only way to construct the type_info object is to use the typeid operator.
what is containd in the type_info class is implementation dependant. the name() const char * is the only member guaranteed to be provided by all C++ implementation , though it is possible that implementaion dependant may provide additional support. what can be added, basically any information that a compiler can provide about a type about a type can be added.
- a list of the class member functions.
- what the layout of an object of this clases type looks like in storage, that is , how the member and base subobjects are mapped
class extended_type_info :public type_info {}; typedef extended_type_info eti; void func(employee * p) { // downcast from type_info * to extended_type_info* if (const eti * eti_p = dynamic_cast<const eti *>(&typeid(*p))) { // if dynamic_cast succeeds // use extended_type_info information through eti_p } else{ // if dynamically fails // use standard type_info information } }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15288首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 965RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 924As in my prevoius example in j ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 738There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 784we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 917As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 724There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 934Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 928As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 951As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1290in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 908class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 960template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1183There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 807the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 555The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1136In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 839A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1133You can overload the subscript ...
相关推荐
最近看了boost::any类源码,其实现主要依赖typeid操作符。很好奇这样实现的时间和空间开销有多大,决定探一下究竟。 VS2008附带的type_info类只有头文件,没有源文件,声明如下: class type_info { public: ...
C++关键字大全(67个) asm auto bad_cast bad_typeid bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum except explicit extern false finally float ...
【原书名】 C++ Primer (4th Edition) 【原出版社】 Addison Wesley/Pearson 【作者】 (美)Stanley B.Lippman,Josée LaJoie,Barbara E.Moo 【译者】 李师贤 蒋爱军 梅晓勇 林瑛 【丛书名】 图灵计算机科学丛书 ...
operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try type_info typedef typeid typename union unsigned ...
Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 Template Template的“具现”行为...
Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 Template Template的“具现”行为...
Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 Template Template的“具现”行为...
if(typeid(me).before(typeid(you))) //... 那么表示我们正在查询m e在排列顺序中是否在y o u之前。 RT T I的第二个用法叫“安全类型向下映射”。之所以用“向下映射”这个词也是由于类继 承的排列顺序。如果映射一...
type_info 类 type_info 类描述编译器在程序中生成的类型信息。此类的对象可以有效存储指向类型的名称的指针。 type_info 类还可存储适合比较两个类型是否相等或比较其排列顺序的... _CRTIMP_PURE bool operator==(con
template <typename>void operator()(T*p)const { os << "deleting unique_ptr " <<typeid(T).name() <<endl; delete p; } private: ostream &os; }; void demo_general_class_tempalte_...