November 27 2006

本文详细介绍了C++中四种类型转换运算符:dynamic_cast、static_cast、reinterpret_cast和const_cast的功能与使用方法。通过这些运算符,开发者可以进行类型安全的向下转型、不同类型的显式转换等操作。
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.
根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值