// static_cast_Operator.cpp// compile with: /LDclassB{};classD:public B {};voidf(B* pb, D* pd){
D* pd2 =static_cast<D*>(pb);// Not safe, D can have fields// and methods that are not in B.
B* pb2 =static_cast<B*>(pd);// Safe conversion, D always// contains all of B.}
与 dynamic_cast 不同,pb 的 static_cast 转换不执行运行时检查。 由 pb 指向的对象可能不是 D 类型的对象,在这种情况下使用 *pd2 会是灾难性的。 例如,调用 D 类(而非 B 类)的成员函数可能会导致访问冲突。
该行为也适用于类以外的类型。 例如,static_cast 可用于将 int 转换为 char。 但是,得到的 char 可能没有足够的位来保存整个 int 值。 同样,这需要程序员来验证 static_cast 转换的结果是否安全。
dynamic_cast 常用语不需要引起异常,而是把转换失败当做一个普通操作的情况。Use dynamic_cast when you cast an object (more specifically, a hat ^) to a more derived type, you expect either that the target object might sometimes be nullptr or that the cast might fail, and you want to handle that condition as a regular code path instead of an exception.
如果 type-id 为 void*,则做运行时进行检查确定 expression的实际类型。 结果是指向 by expression 的完整的对象的指针。 例如:
// dynamic_cast_2.cpp// compile with: /c /GRclassA{virtualvoidf();};classB{virtualvoidf();};voidf(){
A* pa =new A;
B* pb =new B;void* pv =dynamic_cast<void*>(pa);// pv now points to an object of type A
pv =dynamic_cast<void*>(pb);// pv now points to an object of type B}
// dynamic_cast_1.cpp// compile with: /cclassB{};classC:public B {};classD:public C {};voidf(D* pd){
C* pc =dynamic_cast<C*>(pd);// ok: C is a direct base class// pc points to C subobject of pd
B* pb =dynamic_cast<B*>(pd);// ok: B is an indirect base class// pb points to B subobject of pd}
// dynamic_cast_3.cpp// compile with: /c /GRclassB{virtualvoidf();};classD:public B {virtualvoidf();};voidf(){
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}
reinterpret_cast
reinterpret_cast < type-id > ( expression )
Allows any pointer to be converted into any other pointer type
Allows any integral type to be converted into any pointer type
When the cast is invalid, reinterpret_cast might return a non-null value, it is dangerous
On the contrary, dynamic_cast will return nullptr when the cast is invalid
The reinterpret_cast allows the pointer to be treated as an integral type.
const_cast
const_cast (expression)
Removes the const, volatile, and __unaligned attribute(s) from a class
The const_cast operator converts a null pointer value to the null pointer value of the destination type.
You cannot use the const_cast operator to directly override a constant variable’s constant status.
const_cast转换后的结果仍然指向原对象。根据被转换对象的类型不同,转换后的写操作可能引起位置的行为。For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.
const_cast仅在其位于的那句语句中生效,下一条语句后,原const对象还是const属性。The cast lasts only for the remainder of the statement in which it appears.