• <bdo id='siwpu'></bdo><ul id='siwpu'></ul>

    <small id='siwpu'></small><noframes id='siwpu'>

    <legend id='siwpu'><style id='siwpu'><dir id='siwpu'><q id='siwpu'></q></dir></style></legend>
    <i id='siwpu'><tr id='siwpu'><dt id='siwpu'><q id='siwpu'><span id='siwpu'><b id='siwpu'><form id='siwpu'><ins id='siwpu'></ins><ul id='siwpu'></ul><sub id='siwpu'></sub></form><legend id='siwpu'></legend><bdo id='siwpu'><pre id='siwpu'><center id='siwpu'></center></pre></bdo></b><th id='siwpu'></th></span></q></dt></tr></i><div id='siwpu'><tfoot id='siwpu'></tfoot><dl id='siwpu'><fieldset id='siwpu'></fieldset></dl></div>

  • <tfoot id='siwpu'></tfoot>

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

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

          • <bdo id='0RxiJ'></bdo><ul id='0RxiJ'></ul>
              <tbody id='0RxiJ'></tbody>

            <i id='0RxiJ'><tr id='0RxiJ'><dt id='0RxiJ'><q id='0RxiJ'><span id='0RxiJ'><b id='0RxiJ'><form id='0RxiJ'><ins id='0RxiJ'></ins><ul id='0RxiJ'></ul><sub id='0RxiJ'></sub></form><legend id='0RxiJ'></legend><bdo id='0RxiJ'><pre id='0RxiJ'><center id='0RxiJ'></center></pre></bdo></b><th id='0RxiJ'></th></span></q></dt></tr></i><div id='0RxiJ'><tfoot id='0RxiJ'></tfoot><dl id='0RxiJ'><fieldset id='0RxiJ'></fieldset></dl></div>
                <legend id='0RxiJ'><style id='0RxiJ'><dir id='0RxiJ'><q id='0RxiJ'></q></dir></style></legend>
              1. <small id='0RxiJ'></small><noframes id='0RxiJ'>

                1. <tfoot id='0RxiJ'></tfoot>
                  本文介绍了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++ 中的类型转换究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                    <small id='AnRIh'></small><noframes id='AnRIh'>

                        • <tfoot id='AnRIh'></tfoot>
                        • <i id='AnRIh'><tr id='AnRIh'><dt id='AnRIh'><q id='AnRIh'><span id='AnRIh'><b id='AnRIh'><form id='AnRIh'><ins id='AnRIh'></ins><ul id='AnRIh'></ul><sub id='AnRIh'></sub></form><legend id='AnRIh'></legend><bdo id='AnRIh'><pre id='AnRIh'><center id='AnRIh'></center></pre></bdo></b><th id='AnRIh'></th></span></q></dt></tr></i><div id='AnRIh'><tfoot id='AnRIh'></tfoot><dl id='AnRIh'><fieldset id='AnRIh'></fieldset></dl></div>

                            <tbody id='AnRIh'></tbody>

                          <legend id='AnRIh'><style id='AnRIh'><dir id='AnRIh'><q id='AnRIh'></q></dir></style></legend>
                            <bdo id='AnRIh'></bdo><ul id='AnRIh'></ul>