究竟是什么在C / C ++类型转换? [英] What exactly is a type cast in C/C++?

查看:168
本文介绍了究竟是什么在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;

如果我没有记错的双重价值需要更多的空间(它是8个字节?)不是一个整数(4字节)。和两者的内部重新presentation是完全不同的(补充的两个/尾数)。那么,什么内部发生?这里的例子是相当简单的,但在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吧?

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

推荐答案

一个类型转换基本上是从一种类型到另一种转换。它可以是隐式的(即,由编译器自动完成,或者失去在这一过程中的信息)或明确的(即,通过在code中的显影剂指定)。由类型所占用的空间是次要的。更重要的是转换的适用性(有时convenice)。

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_cast 的dynamic_cast reinter pret_cast 的const_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.

有关该铸件由标准允许的和完整的规则并不可以得到相当复杂。我试图忠实present的一些在这个答案的规则有点简明摘要。如果你是在是什么,是不允许真正感兴趣的,我强烈建议您访问标准和类型转换阅读相关章节。

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 ++类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆