type(myVar)和(type)myVar有什么区别? [英] What's the difference between type(myVar) and (type)myVar?

查看:150
本文介绍了type(myVar)和(type)myVar有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在cplusplus.com上阅读完整的教程,并手动编码和编译每个示例。经常,我偶然发现一些令我困惑的东西。

I'm going through the full tutorial at cplusplus.com, coding and compiling each example manually. Regularly, I stumble upon something that leaves me perplexed.

我目前正在学习本节: http://www.cplusplus.com/doc/tutorial/structures/ 。仅阅读本教程,就可以轻松忽略一些细微之处。手动键入所有内容的优点是这样的细节确实突出。

I am currently learning this section: http://www.cplusplus.com/doc/tutorial/structures/ . There are some subtleties that could easily be overlooked by only reading the tutorial. The advantage of typing everything by hand is that such details do stand out.

在上一页中,有两个示例程序。其中一行如下:

In the above page, there are two sample programs. One has this line:

stringstream(mystr) >> yours.year;

另一行有此行:

(stringstream) mystr >> pmovie->year;

我不了解的是类型之间的区别(如果有) (myVar)= x; (type)myVar = x;

我没有按顺序进行整个教程。我检查了一下,但没有找到解决的地方,尽管我可能错过了。

I am not doing the whole tutorial in sequential order. I checked but didn't find this addressed anywhere, though I may have missed it.


  • 有什么区别吗?

  • 是否存在一种首选方法而不是另一种方法?

推荐答案

type(x)之间没有区别(type)x 。这两个是完全等效的。对于类,大多数人更喜欢 type(x),对于非类类型,更喜欢(type)x ,但这纯粹是对的自己选择。两者都调用带有一个参数 x 的类的构造函数。

There is no difference between type(x) and (type)x. These two are completely equivalent. Most people prefer type(x) for classes and (type)x for non-class types, but that's purely up to one's own choice. Both call constructors for classes with one argument x.

类的首选方式是 type(x),因为这允许向构造函数传递多个参数,例如 type(x,y)。尝试应用其他形式(type)x,y 将不起作用:它强制转换 x ,然后应用逗号运算符,并单独评估 y 。像(type)(x,y)这样的括号无济于事:这将评估 x y ,然后将 y 转换为 type

The preferred way for classes is type(x), because this allows passing more than one argument to the constructor, as in type(x, y). Trying to apply the other form, (type)x, y will not work: It casts x, and then applies the comma operator and evalutes y in isolation. Parentheses like (type)(x, y) do not help: This will evaluate x and y in isolation using the comma operator and then cast y to type.

对于非类类型,这样的强制转换通常过于强大。 C ++具有 static_cast< type>(x),用于大致执行隐式转换的逆向操作(例如将基类转换为派生类并转换 void * 指向另一个指针),通常适合。请参见何时应使用static_cast,dynamic_cast和reinterpret_cast?

For non-class types, such a cast is often too powerful. C++ has static_cast<type>(x) for roughly doing the reverse of an implicit conversion (such as casting base classes to derived classes and casting void* to another pointer), which often is what fits in. See When should static_cast, dynamic_cast and reinterpret_cast be used?.

stringstream 不是函数。执行 function(x)会将该函数称为函数,但是执行(function)x 是非法的,因为有两个表达式彼此相邻,中间没有运算符。

stringstream is not a function, though. Doing function(x) will call it the function, but doing (function)x is illegal, beause there are two expressions next to each other, with no operator in between.

对于那些不相信这个答案,并且对直觉感到不满的人,请查阅 5.2.3 / 1

For those who don't believe this answer, and downvote it on gut feeling, please consult the Standard at 5.2.3/1


后面是一个简单类型说明符(7.1.5)括起来的表达式列表通过给定表达式列表构造指定类型的值。如果表达式列表是单个表达式,则类型转换表达式与相应的转换表达式(5.4)等效(定义正确,含义明确)。

A simple-type-specifier (7.1.5) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4).

这篇关于type(myVar)和(type)myVar有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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