- 浏览: 398979 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15338首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1008RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 768It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 955As in my prevoius example in j ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 768There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 817we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 975As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 751There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 959Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 974As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 993As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1327in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 935class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 985template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1208There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 849the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 585The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1164In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 867A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1183You can overload the subscript ...
相关推荐
在C++编程语言中,`typeid`是一个非常重要的运行时类型信息(RTTI,Runtime Type Information)的关键字。它允许程序员在程序运行时查询对象或指针的实际类型,这对于多态性和动态类型检查非常有用。在本教程中,...
22. `operator`:`operator`用于重载运算符,使用户自定义类型支持标准运算符。 23. `private`、`protected`、`public`:这三个关键字定义类的成员访问权限,分别代表私有、受保护和公有。 24. `register`:请求...
: , sizeof , typeid 这几个运算符不能被重载,其他运算符都能被重载。 ### 重载不能改变该运算符用于内置类型时的函义 程序员不能改变运算符+用于两个int型时的含义。 ### 运算符函数的参数至少有一个必须是类...
总的来说,`type_info`类和`bad_typeid`异常是C++运行时类型信息基础设施的重要组成部分,它们使得在程序运行过程中能够检查和处理不同类型的数据,从而增强代码的灵活性和安全性。理解这些概念对于深入学习C++的...
### C++中不可重载的运算符及其规则 在C++编程语言中,为了提高代码的灵活性和可读性,提供了运算符重载这一特性。然而,并非所有的运算符都可以被重载。本文将详细介绍那些不可重载的运算符以及自增(`++`)和自减(`...
:`、`sizeof`、`typeid`。 3. 不能改变运算符的参数个数、优先级或结合性。 4. 重载运算符不能使用默认参数。 运算符重载的语法通常形式为`返回类型 operator@( 参数表 )`,其中`@`代表要重载的运算符。对于类内的...
最近看了boost::any类源码,其实现主要依赖typeid操作符。很好奇这样实现的时间和空间开销有多大,决定探一下究竟。 VS2008附带的type_info类只有头文件,没有源文件,声明如下: class type_info { public: ...
`typeid`操作符是C++中用于实现RTTI的主要方式之一。它接收一个表达式作为参数,并返回一个表示该表达式类型的`std::type_info`对象。这个对象包含了类型的信息,包括类型的名字。 - **语法**: `std::type_info & ...
- **operator**(运算符重载):允许我们自定义操作符的行为。 - **private**(私有):限定符,用于限制对类成员的访问。 - **protected**(受保护):限定符,介于`private`和`public`之间,子类可以访问父类的受...
### C++关键字详解 在C++编程语言中,关键字具有特定的语法意义,它们由编译器识别并赋予特殊含义。下面将详细介绍一系列C++的关键字及其用途。 #### asm(汇编指令) - **功能**:`asm`关键字用于嵌入汇编代码到...
Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 Template Template的“具现”行为...
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 ...
特定操作符如`operator=`、`operator[]`、`operator()`和`operator->`只能是成员函数。 4. **操作符`->`** 返回值应是类指针或支持`->`操作的对象。 5. **自增/自减操作符** `operator++`和`operator--`,前缀形式...
- ANSI C++独有的关键词:`bool`、`catch`、`class`、`const_cast`、`delete`、`dynamic_cast`、`explicit`、`false`、`friend`、`inline`、`mutable`、`namespace`、`new`、`nullptr`、`operator`、`private`、`...
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 ...
- 更多C++关键字包括`explicit`、`friend`、`mutable`、`new`、`operator`、`private`、`protected`、`public`、`reinterpret_cast`、`return`、`static_cast`、`template`、`this`、`throw`、`try`、`typeid`、`...
* operator * this * wchar_t * continue * float * private * throw * while * default * friend * protected * true 提示:在给变量或者常量起名称时候,不要用C++的关键字,否则会产生歧义。 1.6 标识符命名...