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

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

问题描述

我正在通过cplusplus.com的完整教程,手动编码和编译每个示例。



我目前正在学习本节: http://www.cplusplus.com/doc/tutorial/structures/ 。有一些细微之处,只是阅读教程很容易忽略。手动输入所有内容的好处是,这些细节非常突出。



在上面的页面中,有两个示例程序。有这行:

  stringstream(mystr)>>你的年龄

另一个有这一行:

 (stringstream)mystr>> pmovie-> year; 

我不明白是函数之间的区别(myVar)= x; (function)myVar = x;



我不是按顺序的整个教程。我检查了,但没有发现这在任何地方,虽然我可能错过了。




  • 有什么区别吗?

  • 有没有首选方法,而不是其他方式? type(x) 没有区别 code>和(type)x 。这两个是完全等价的。大多数人喜欢 type(x)用于类,(type)x 用于非类类型,自己选择。两个调用具有一个参数 x 的类的构造函数。



    类的首选方法是 type(x),因为这允许传递多个参数到构造函数,如 type(x,y)中所示。尝试应用其他形式,(type)x,y 将无法工作:它转换 x 逗号运算符和 y (类型)(x,y)的括号不帮助:这将评估 x y 隔离使用逗号运算符,然后将 y 转换为类型



    对于非类类型,这样的类型通常太强大。 C ++有 static_cast< type>(x)用于粗略地执行隐式转换的反向(例如将基类转换为派生类,并转换 void * 到另一个指针),这通常是适合的。参见什么时候应该使用static_cast,dynamic_cast和reinterpret_cast?



    stringstream 不是函数。做 function(x)会调用它的函数,但是(function)x 是非法的,表达式彼此相邻,之间没有运算符。






    对于那些不相信这个答案,以及在直觉上感觉不好的人,请参阅标准 5.2.3 / 1


    通过括号表达式列表构造给定表达式列表的指定类型的值。如果表达式列表是单个表达式,则类型转换表达式与相应的强制转换表达式(5.4)是等效的(在定义中,如果定义的话)。



    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.

    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;
    

    The other one has this line:

    (stringstream) mystr >> pmovie->year;
    

    What I don't understand is the difference (if any) between function (myVar) = x; and (function) 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.

    • Is there a difference?
    • Is there a preferred way to do it one way rather than the other?

    解决方案

    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.

    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.

    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 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.


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

    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).

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

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