为什么要铸造避免? [英] Why should casting be avoided?

查看:100
本文介绍了为什么要铸造避免?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般避免铸造类型尽可能因为我的IM pression它的编码习惯差,可能会导致性能罚下。

I generally avoid casting types as much as possible since I am under the impression that it's poor coding practice and may incur a performance penalty.

但是,如果有人问我解释到底为什么也就是说,我可能会看他们像大灯鹿。

But if someone asked me to explain why exactly that is, i would probably look at them like a deer in headlights.

那么,为什么/时,是铸造不好?

So why/when is casting bad?

是不是一般的Java,C#,C ++或做每个不同的运行环境处理它在它自己的条款?

Is it general for java, c#, c++ or does every different runtime environment deal with it on it's own terms?

对于任何语言相关的欢迎,比如它为什么坏C ++?

Specifics for a any language are welcome, example why is it bad in c++?

推荐答案

您已经用三种语言标记这一点,答案是真的三者之间有很大不同。的C ++讨论或多或少暗示的C讨论蒙上为好,这给了(或多或少)第四个答案。

You've tagged this with three languages, and the answers are really quite different between the three. Discussion of C++ more or less implies discussion of C casts as well, and that gives (more or less) a fourth answer.

由于它是你没有明确提及的人,我将开始与C.ç蒙上有许多问题。之一是,他们可以做任何数目的不同的事物的。在某些情况下,中投确实无非告诉编译器(本质):闭嘴,我知道我在做什么 - 也就是说,它可以确保即使当你做一个转换,可能导致出现问题,编译器不会警告你这些潜在的问题。只是举例,的char a =(char)的123456; 。定义这个实现的精确结果(取决于大小和带符号字符),并且只是在一些非常奇怪的情况,很可能是没有用的。 ç蒙上也无论他们是发生的事情只有在编译时间而变化(即,你只是告诉编译器如何跨preT /治疗某些数据)发生在运行时间或东西(例如,一个实际的从转换到双长)。

Since it's the one you didn't mention explicitly, I'll start with C. C casts have a number of problems. One is that they can do any of a number of different things. In some cases, the cast does nothing more than tell the compiler (in essence): "shut up, I know what I'm doing" -- i.e., it ensures that even when you do a conversion that could cause problems, the compiler won't warn you about those potential problems. Just for example, char a=(char)123456;. The exact result of this implementation defined (depends on the size and signedness of char), and except in rather strange situations, probably isn't useful. C casts also vary in whether they're something that happens only at compile time (i.e., you're just telling the compiler how to interpret/treat some data) or something that happens at run time (e.g., an actual conversion from double to long).

的C ++试图通过将若干新的转换运算符,其中每一个被限制为仅一个C铸件的功能的子集来处理该至少某种程度。这使得它更加困难(例如)小心你真的不打算转换 - 如果你的只有的打算抛弃常量性的对象上,你可以使用 const_cast会,并确保的只有的东西它可以影响一个对象是否常量挥发性,还是不行。相反,的static_cast 不得影响对象是否常量挥发性。总之,你有大部分同类型的能力,但他们归类,一个铸造一般只能做一种转换,在一个单一的C样式转换可以在一个操作中做两或三个转换。主要的例外是,您的可以的使用的dynamic_cast 代替的的static_cast 在至少某些情况下,尽管被写成的dynamic_cast ,它会真的结束了作为一个的static_cast 。例如,你可以使用的dynamic_cast 来遍历向上或向下一个类层次结构 - 但铸造向上的层次结构始终是安全的,所以可以静态地完成,而所以它的动态做了投下的层次并不一定是安全的。

C++ attempts to deal with that to at least some extent by adding a number of "new" cast operators, each of which is restricted to only a subset of the capabilities of a C cast. This makes it more difficult to (for example) accidentally do a conversion you really didn't intend -- if you only intend to cast away constness on an object, you can use const_cast, and be sure that the only thing it can affect is whether an object is const, volatile, or not. Conversely, a static_cast is not allowed to affect whether an object is const or volatile. In short, you have most of the same types of capabilities, but they're categorized so one cast can generally only do one kind of conversion, where a single C-style cast can do two or three conversions in one operation. The primary exception is that you can use a dynamic_cast in place of a static_cast in at least some cases and despite being written as a dynamic_cast, it'll really end up as a static_cast. For example, you can use dynamic_cast to traverse up or down a class hierarchy -- but a cast "up" the hierarchy is always safe, so it can be done statically, while a cast "down" the hierarchy isn't necessarily safe so it's done dynamically.

Java和C#得多彼此相似。特别是,他们两人铸造(几乎?)始终是运行时操作。在C ++中投运营商而言,它通常是最接近的dynamic_cast 在什么是真正做到方面 - 例如,当您试图将对象强制转换为某种目标类型,编译器插入一个运行时检查,看该转换是否允许,并抛出一个异常,如果它不是。确切的细节(例如,用于坏投异常的名字)各不相同,但基本原理仍然大多相似(虽然,如果没有记错,Java没有做出适用于一些非对象类型的演员像 INT 更接近到C蒙上 - 但是这些类型很少使用,以至于1)我不记得那是肯定的,和2),即使这是真的,它没有多大意义反正)。

Java and C# are much more similar to each other. In particular, with both of them casting is (virtually?) always a run-time operation. In terms of the C++ cast operators, it's usually closest to a dynamic_cast in terms of what's really done -- i.e., when you attempt to cast an object to some target type, the compiler inserts a run-time check to see whether that conversion is allowed, and throw an exception if it's not. The exact details (e.g., the name used for the "bad cast" exception) varies, but the basic principle remains mostly similar (though, if memory serves, Java does make casts applied to the few non-object types like int much closer to C casts -- but these types are used rarely enough that 1) I don't remember that for sure, and 2) even if it's true, it doesn't matter much anyway).

在看东西更普遍,这种情况的pretty简单的(至少IMO):铸件(显然不够)意味着你从一种类型转换的东西到另一个。当/如果你这样做,它提出了一个问题:为什么?如果你真的想要的东西是一种特殊类型的,你为什么不把它定义为是此类型入手?这并不是说有的从不的理由做这样的转换,但是任何时候发生,就应该提示是否可以重新设计code中的问题,以便正确的类型在整个使用。即使是看似无害的转换(例如,整数和浮点之间)应该更加紧密地比常见检查。尽管他们的似乎的相似性,整数真的应该用于计算类型的东西和测量各种各样的东西浮点。忽略的区别是什么导致了一些疯狂之类的语句的美国家庭平均有1.8个孩子。尽管我们都可以看到这种情况发生,但事实是,的没有的家庭有1.8个孩子。他们可能有1或搞不好2或者他们可能有不止于此 - 但从未1.8

Looking at things more generally, the situation's pretty simple (at least IMO): a cast (obviously enough) means you're converting something from one type to another. When/if you do that, it raises the question "Why?" If you really want something to be a particular type, why didn't you define it to be that type to start with? That's not to say there's never a reason to do such a conversion, but anytime it happens, it should prompt the question of whether you could re-design the code so the correct type was used throughout. Even seemingly innocuous conversions (e.g., between integer and floating point) should be examined much more closely than is common. Despite their seeming similarity, integers should really be used for "counted" types of things and floating point for "measured" kinds of things. Ignoring the distinction is what leads to some of the crazy statements like "the average American family has 1.8 children." Even though we can all see how that happens, the fact is that no family has 1.8 children. They might have 1 or they might 2 or they might have more than that -- but never 1.8.

这篇关于为什么要铸造避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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