February 2nd Monday 2009 (二月 二日 月曜日)

来源:互联网 发布:网络软文收费 编辑:程序博客网 时间:2024/04/29 18:13

 
  Today is the first work day in Chinese new year.  During the Spring Festival holiday, I reviewed the principle of the compile.
The long holiday is precious for me.

C++ has three components supporting RTTI:

  * The dynamic_cast operator generates a pointer to a derived type from a pointer to a base type, if possible. Otherwise, the
  operator returns 0, the null pointer.

  * The typeid operator returns a value identifying the exact type of an object.

  * A type_info structure holds information about a particular type.

  You can use RTTI only with a class hierarchy having virtual functions.  The reason for this is that these are the only class
hierarchies for which you should be assigning the addresses of derived objects to base class pointers.

  RTTI works only for classes with virtual functions.

dynamic_cast<Type *>(pt)

  converts the pointer pt to a pointer of type Type * if the pointed-to object (*pt) is of type Type or else derived directly
or indirectly from type Type. Otherwise, the expression evaluates to 0, the null pointer.

  The purpose of this operator is to allow upcasts within a class hierarchy (such type casts being safe because of the is-a relationship)
and to disallow other casts.

The typeid Operator and type_info Class

  The typeid operator lets you determine if two objects are the same type. Somewhat like sizeof, it accepts two kinds of arguments:

  * The name of a class

  * An expression that evaluates to an object

  The typeid operator returns a reference to a type_info object, where type_info is a class defined in the typeinfo header file
(formerly typeinfo.h). The type_info class overloads the == and != operators so that you can use these operators to compare types.
For example, the expression

  typeid(Magnificent) == typeid(*pg)

evaluates to the bool value true if pg points to a Magnificent object and to false otherwise. If pg happens to be a null pointer,
the program will throw a bad_typeid exception. This exception type is derived from the exception class and is declared in the typeinfo
header file.

  The implementation of the type_info class will vary among vendors, but it will include a name() member that returns an implementation-dependent
string that typically would be the name of the class.

static_cast < type-name > (expression)

  It's valid only if type_name can be converted implicitly to the same type expression has, or vice versa. Otherwise, the cast is an error. Suppose
High is a base class to Low and that Pond is an unrelated class.

const_cast < type-name > (expression)

  The result making such a cast is an error if any other aspect of the type is altered. That is, type_name and expression must be of the same type
except they can differ in the presence or absence of const or volatile.

High bar;
const High * pbar;
    ...
High * pb = const_cast<High *> (pbar);    // valid
Low * pl = const_cast<Low *> (pbar);      // invalid

原创粉丝点击