本文介绍了C型铸件和C型铸件之间的区别是什么?功能铸件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





b = (int) a;    // c-like cast notation
b = int (a);    // functional notation


推荐答案

显然我错了,它们大致相同。虽然 long long void * 的复合类型名称可以直接使用函数语法(即 long long(val)不起作用),使用 typedef 可以解决这个问题。

Apparently I was wrong in my initial cut at an answer. They are roughly equivalent. And while compound type names like long long or void * can use functional syntax directly (i.e. long long(val) doesn't work), using typedef can get around this issue.

两个演员表都很糟糕,应该避免。例如:

Both cast notations are very bad and should be avoided. For example:

const char c = 'a';
void *fred = (void *)(&c);

有效,但不应该。

两个C风格的表示法有时会像 static_cast ,有时像 const_cast ,有时像 reinterpret_cast ,甚至两者的组合,具体取决于它的使用的确切情况。这些语义是相当复杂的,并不总是很容易告诉究竟在任何给定情况下发生了什么。

Both the C-style cast notation will sometimes behave like static_cast, sometimes like const_cast, sometimes like reinterpret_cast, or even a combination of the two depending on the exact situation in which it's used. These semantics are rather complex and it's not always easy to tell exactly what's happening in any given situation.

我已经使用主要是C ++ static_cast<类型>(val)样式转换,并且从不使用C风格的转换。基于我对这个问题的研究,我也将停止使用函数式铸造任何东西。问题有一个很好的答案(接受的),详细为什么。

I have gone to using mostly C++ static_cast<type>(val) style casts, and never use C-style casts. Based on my research for this question I'm going to also stop using function-style casts for anything. The question "C++ cast syntax styles" has an excellent answer (the accepted one) that details why.

这篇关于C型铸件和C型铸件之间的区别是什么?功能铸件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 10:26