C型铸造和功能铸造之间有什么区别? [英] What is the difference between C-like casting and functional casting?

查看:67
本文介绍了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't 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样式强制转换。根据对这个问题的研究,我也将停止对任何东西使用函数样式转换。问题 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型铸造和功能铸造之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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