CAST operation
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.
If type-id is void*, a run-time check is made to determine the actual type of expression. The result is a pointer to the complete object pointed to by expression.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.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.
The static_cast operator can also be used to perform any implicit conversion, including standard conversions and user-defined conversions
template< class T >
class lockable_var_t : public T, public lock_t
{
public:
lockable_var_t& operator=(const T& t)
{
static_cast<T&>(*this)=t;
}
};
和dynamic_cast一样,会根据类型信息进行地址的转换计算,只是没有安全检查
const_cast < type-id > ( expression )
The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.
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.
The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type.
和dynamic_cast、static_cast不同,不进行地址的转化,只是对同一地址重新解释为另外的类型
posted on 2004-07-22 15:58 哲学 艺术 程序 人生 阅读(458) 评论(0) 收藏 举报
浙公网安备 33010602011771号