const_cast Operator
const_cast < type-id > ( expression )
The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.
用于取消变量的const、volatile、__unaligned属性。
详见msdn:http://msdn.microsoft.com/en-us/library/bz6at95h(v=vs.71).aspx
dynamic_cast Operator
dynamic_cast < type-id > ( expression )
The expression dynamic_cast<type-id>( expression ) converts the operand expression to an object of type type-id. The type-id must be a pointer or a reference to a previously defined class type or a "pointer to void". The type of expression must be a pointer if type-id is a pointer, or an l-value if type-id is a reference.
常用于子类到父类指针间的转换,有a run-time check。
Msdn摘录:
If the type of expression is a base class of the type of type-id, a run-time check is made to see if expression actually points to a complete object of the type of type-id. If this is true, the result is a pointer to a complete object of the type of type-id. For example:
class B { ... };
class D : public B { ... };
void f()
{
B* pb = new D; // unclear but ok
B* pb2 = new B;
D* pd = dynamic_cast<D*>(pb); // ok: pb actually points to a D
...
D* pd2 = dynamic_cast<D*>(pb2); // pb2 points to a B not a D
// cast was bad so pd2 == NULL
...
}
详见msdn:http://msdn.microsoft.com/en-us/library/cby9kycs(v=vs.71).aspx
static_cast Operator,相对于dynamic_cast Operator
static_cast < type-id > ( expression )
The static_cast operator converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.
常用于子类到父类指针间的转换,没有a run-time check。
The dynamic_cast and static_cast operators move a pointer throughout a class hierarchy. However, static_cast relies exclusively on the information provided in the cast statement and can therefore be unsafe.
For example:
class B { ... };
class D : public B { ... };
void f(B* pb, D* pd)
{
D* pd2 = static_cast<D*>(pb); // not safe, pb may
// point to just B
B* pb2 = static_cast<B*>(pd); // safe conversion
...
}
详见msdn:http://msdn.microsoft.com/en-us/library/c36yw7x9(v=vs.71).aspx
reinterpret_cast Operator
reinterpret_cast < type-id > ( expression )
The reinterpret_cast operator allows any pointer to be converted into any other pointer type. It also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators.
近似于无理由的转换,但不安全。
详见msdn:http://msdn.microsoft.com/en-us/library/e0w9f63b(v=vs.71).aspx