C/C++ 中的类型转换究竟是什么?

What exactly is a type cast in C/C++?(C/C++ 中的类型转换究竟是什么?)

本文介绍了C/C++ 中的类型转换究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C/C++ 中的类型转换究竟是什么?编译器如何检查是否需要(并且有效)显式类型转换?它是否比较了一个值所需的空间?如果我有例如:

What exactly is a type cast in C/C++? How does the compiler check if an explicit typecast is needed (and valid)? Does it compare the space required for an value? If I have for example:

int a;
double b = 15.0;
a = (int) b;

如果我没记错的话,double 值需要比整数(4 字节)更多的空间(是 8 字节吗?!).并且两者的内部表示完全不同(对二/尾数进行补码).那么内部会发生什么?这里的例子很简单,但在 C/C++ 中有很多类型转换.

If I remember correctly a double value requires more space (was it 8 bytes?!) than an integer (4 bytes). And the internal representation of both are completely different (complement on two/mantissa). So what happens internally? The example here is quite straightforward, but in C/C++ there are plentiful typecasts.

编译器如何知道(或程序员)我是否可以转换,例如FOO 到 BAR?

How does the compiler know (or the programmer) if I can cast e.g. FOO to BAR?

推荐答案

类型转换基本上是从一种类型到另一种类型的转换.它可以是隐式的(即,由编译器自动完成,可能会在过程中丢失信息)或显式的(即,由开发人员在代码中指定).类型占用的空间是次要的.更重要的是转换的适用性(有时是方便).

A type cast is basically a conversion from one type to another. It can be implicit (i.e., done automatically by the compiler, perhaps losing info in the process) or explicit (i.e., specified by the developer in the code). The space occupied by the types is of secondary importance. More important is the applicability (and sometimes convenice) of conversion.

隐式转换可能会丢失信息,可能会丢失/获得符号,并且可能会发生上溢/下溢.编译器不会保护您免受这些事件的影响,除非通过在编译时生成的警告.当派生类型隐式转换为基类型(按值)时,也可能发生切片.

It is possible for implicit conversions to lose information, signs can be lost / gained, and overflow / underflow can occur. The compiler will not protect you from these events, except maybe through a warning that is generated at compile time. Slicing can also occur when a derived type is implicitly converted to a base type (by value).

对于可能非常危险的转换(例如,从基础类型到派生类型),C++ 标准需要显式转换.不仅如此,它还提供了更多限制性的显式转换,例如 static_castdynamic_castreinterpret_castconst_cast,每个都进一步将显式转换限制为仅可能转换的一个子集,从而减少转换错误的可能性.

For conversions that can be downright dangerous (e.g., from a base to a derived type), the C++ standard requires an explicit cast. Not only that, but it offers more restrictive explicit casts, such as static_cast, dynamic_cast, reinterpret_cast, and const_cast, each of which further restricts the explicit cast to only a subset of possible conversions, reducing the potential for casting errors.

隐式和显式的有效转换最终由 C/C++ 标准定义,尽管在 C++ 中,开发人员可以通过使用构造函数和重载(cast) 运算符.

Valid conversions, both implicit and explict are ultimately defined by the C/C++ standards, although in C++, the developer has the ability to extend conversions for user defined types, both implicit and explicit, via the use of constructors and overloaded (cast) operators.

标准允许和不允许的完整规则可能会变得非常复杂.我试图在这个答案中忠实地对其中的一些规则进行一些简明的总结.如果您真的对允许和不允许的内容感兴趣,我强烈建议您访问标准并阅读有关类型转换的相应部分.

The complete rules for which casts are allowed by the standards and which are not can get quite intricate. I have tried to faithfully present a somewhat concise summary of some of those rules in this answer. If you are truly interested in what is and is not allowed, I strongly urge you to visit the standards and read the respective sections on type conversion.

这篇关于C/C++ 中的类型转换究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C/C++ 中的类型转换究竟是什么?

基础教程推荐