一般建议使用 " static_cast< SomeType> someInstance"而不是 传统臭名昭着的(SomeType)someInstance。 我应该对简单类型使用相同的练习, 即代替: 双倍分数=(双倍)n /总数; 我应该写这个吗? double fraction = static_cast< double>(n)/ static_cast< double>(总计); 我找到传统版本更具可读性 在这种情况下。 谢谢!
It is a common recommendation to use "static_cast<SomeType> someInstance" instead of the traditional infamous "(SomeType) someInstance". Should I use the same practice for simple types, i.e. instead of: double fraction = (double) n / total; should I write this? double fraction = static_cast<double>(n) / static_cast<double>(total); I find the traditional version a lot more readable in this case. Thanks!
推荐答案*雅各布: 使用" static_cast< SomeType>这是一个常见的建议。 someInstance"而不是传统的臭名昭着的(SomeType)someInstance" 我应该对简单类型使用相同的练习,即代替: double fraction =(double)n / total; 我应该写这个吗? double fraction = static_cast< double>(n)/ static_cast< double> ;(总); 在这种情况下,我发现传统版本更具可读性。 It is a common recommendation to use "static_cast<SomeType> someInstance" instead of the traditional infamous "(SomeType) someInstance". Should I use the same practice for simple types, i.e. instead of: double fraction = (double) n / total; should I write this? double fraction = static_cast<double>(n) / static_cast<double>(total); I find the traditional version a lot more readable in this case.
至少有为什么它是一个好主意(TM)使用C ++ 名为强制转换的简单内置类型的三个原因: *它可以帮助你你的代码的维护者记得一般都不要使用C风格的,这有助于防止意外使用。 * C风格的演员可以做const_cast和reinterpret_cast,你可能只需要b $ b不想要(尤其不是指针值)。 *类型定义可能会改变wh代码被维护。 - 答:因为它弄乱了人们通常阅读文本的顺序。 问:为什么这么糟糕? A:热门发布。 问:usenet和电子邮件中最烦人的是什么?
There are at least three reasons why it''s a Good Idea (TM) to use the C++ named casts also for simple built-in types: * It helps you and maintainers of your code remember to not use C-style casts in general, which can help to prevent accidental usage. * C-style casts can do const_cast and reinterpret_cast, which you probably don''t want (especially not for pointer values). * The type definitions may be changed when the code is maintained. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
" Jacob" < JA *** @ yahoo>在消息中写道 新闻:3M ******************** @ telenor ... "Jacob" <ja***@yahoo> wrote in message news:3M********************@telenor... 它是一个常见的建议使用static_cast< SomeType> someInstance"而不是传统的臭名昭着的(SomeType)someInstance" 我应该对简单类型使用相同的练习,即代替: double fraction =(double)n / total; 我应该写这个吗? double fraction = static_cast< double>(n)/ static_cast< double> ;(总); 在这种情况下,我发现传统版本更具可读性。 这是真的!传统版本比C ++ 新的强制转换操作符感觉更自然。 但这就是为什么C ++强制转换操作符看起来如此:它们是如此丑陋 希望劝阻你并警告你不要使用它们。并且您总是可以在文本编辑器中使用查找来获取所有演员阵容。 谢谢! It is a common recommendation to use "static_cast<SomeType> someInstance" instead of the traditional infamous "(SomeType) someInstance". Should I use the same practice for simple types, i.e. instead of: double fraction = (double) n / total; should I write this? double fraction = static_cast<double>(n) / static_cast<double>(total); I find the traditional version a lot more readable in this case. This is TRUE! The traditional version DOES feel a lot natural than the C++ new cast operators. But this is EXACTLY why C++ cast operators look this way: they are so ugly looking to discourage you and alert you from using them. And you can always pick up all casts using a find in a text editor. Thanks!
是等价的 double fraction =(double)n / total; really double fraction = static_cast< double>( n)/总计; 双倍分数=(双倍)n /(双)总数; 不是那么可读 Is the equivilent of double fraction = (double) n / total; really double fraction = static_cast<double>(n) / total; double fraction = (double) n / (double) total; isn''t that readable either
更多推荐
C ++演员
发布评论