November 27 2006

dynamic_cast Operator

  The dynamic_cast operator performs type conversions at run time. The dynamic_cast operator
guarantees the conversion of a pointer to a base class to a pointer to a derived class, or the
conversion of an lvalue referring to a base class to a reference to a derived class.  A program
can thereby use a class hierarchy safely. This operator and the typeid operator provide run-time
type information (RTTI) support in C++.

  The expression dynamic_cast<T>(v) converts the expression v to type T.  Type T must be a pointer
or reference to a complete class type or a pointer to void.  If T is a pointer and the dynamic_cast
operator fails, the operator returns a null pointer of type T.  If T is a reference and the dynamic_cast
operator fails, the operator throws the exception std::bad_cast.  You can find this class in the
standard library header <typeinfo>.

  The dynamic_cast operator requires run-time type information (RTTI) to be generated, which must
be explicitly specified at compile time through a compiler option.  If T is a void pointer, then
dynamic_cast will return the starting address of the object pointed to by v.

  The primary purpose for the dynamic_cast operator is to perform type-safe downcasts.  A downcast
is the conversion of a pointer or reference to a class A to pointer or reference to a class B,
where class A is a base class of B.  The problem with downcasts is that a pointer of type A* can
and must point to any object of a class that has been derived from A.  The dynamic_cast operator
ensures that if you convert a pointer of class A to a pointer of a class B, the object that A
points to belongs to class B or a class derived from B.

  You may perform downcasts with the dynamic_cast operator only on polymorphic classes.  In the
above example, all the classes are polymorphic because class A has a virtual function. The dynamic_cast
operator uses the run-time type information generated from polymorphic classes.


static_cast Operator

  All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.

  Applying the static_cast operator to a null pointer will convert it to a null pointer value of the
target type.

  You can explicitly convert a pointer of a type A to a pointer of a type B if A is a base class of B.
If A is not a base class of B, a compiler error will result.

  You may cast an lvalue of a type A to a type B& if the following are true:

    * A is a base class of B
    * You are able to convert a pointer of type A to a pointer of type B
    * The type B has the same or greater const or volatile qualifiers than type A
    * A is not a virtual base class of B
The result is an lvalue of type B.

  A pointer to member type can be explicitly converted into a different pointer to member type if both
types are pointers to members of the same class. This form of explicit conversion may also take place
if the pointer to member types are from separate classes, however one of the class types must be derived
from the other.


reinterpret_cast Operator

  A reinterpret_cast operator handles conversions between unrelated types.

  The reinterpret_cast operator produces a value of a new type that has the same bit pattern as its argument.
You cannot cast away a const or volatile qualification. You can explicitly perform the following conversions:

    * A pointer to any integral type large enough to hold it
    * A value of integral or enumeration type to a pointer
    * A pointer to a function to a pointer to a function of a different type
    * A pointer to an object to a pointer to an object of a different type
    * A pointer to a member to a pointer to a member of a different class or type, if the types of the
      members are both function types or object types
    * A null pointer value is converted to the null pointer value of the destination type.

Given an lvalue expression of type T and an object x, the following two conversions are synonymous:

reinterpret_cast<T&>(x)
*reinterpret_cast<T*>(&x)

  ISO C++ also supports C-style casts.  The two styles of explicit casts have different syntax but the same
semantics, and either way of reinterpreting one type of pointer as an incompatible type of pointer is usually
invalid.  The reinterpret_cast operator, as well as the other named cast operators, is more easily spotted
than C-style casts, and highlights the paradox of a strongly typed language that allows explicit casts.

  The C++ compiler detects and quietly fixes most but not all violations.  It is important to remember that
even though a program compiles, its source code may not be completely correct. On some platforms, performance
optimizations are predicated on strict adherence to ISO aliasing rules. Although the C++ compiler tries to
help with type-based aliasing violations, it cannot detect all possible cases.


const_cast Operator

  A const_cast operator is used to add or remove a const or volatile modifier to or from a type.

  Type and the type of expression may only differ with respect to their const and volatile qualifiers.  Their
cast is resolved at compile time. A single const_cast expression may add or remove any number of const or
volatile modifiers.

  The result of a const_cast expression is an rvalue unless Type is a reference type.  In this case, the
result is an lvalue.

  Types can not be defined within const_cast.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值