简体   繁体   English

C ++运算符的优先级“&”和“ - >”

[英]Priority of C++ operators “&” and “->”

Given the following: 鉴于以下内容:

&row->count

Would &(row->count) be evaluated or (&row)->count be evaluated in C++? 是否会评估&(row-> count)或者用C ++评估(&row) - > count?

EDIT: Here's a great link for C++ precedence. 编辑:这是一个很好的C ++优先级链接

As far as precedence rules go, I've always liked the one put forth by Steve Oualline in "Practical C": 至于优先规则,我总是喜欢Steve Oualline在“Practical C”中提出的那个:

There are fifteen precedence rules in C (&& comes before || comes before ?:). C中有十五个优先级规则(&&在||之前出现?:)。 The practical programmer reduces these to two: 实际的程序员将这些减少到两个:

1) Multiplication and division come before addition and subtraction. 1)乘法和除法在加法和减法之前。

2) Put parentheses around everything else. 2)将括号括在其他所有地方。

This is already asked. 这已经被问到了。 But here is a link. 这里是一个链接。

Edit: Ok this question is very similar. 编辑:好的, 这个问题非常相似。 And possibly there is an other one. 可能还有另外一个。

C operator precendence is explained here 这里解释 C运算符的优先级

As per the table, -> is higher priority than the & operator, so it's &(row->count) 根据表格, - >的优先级高于&运算符,因此它是&(row-> count)

May I suggest that you resolve such questions using a test programme? 我建议您使用测试程序解决这些问题吗? That has the advantage that you will know for sure that the answer is correct for your implementation, and you are not exposed to the risk of badly answered questions. 这样做的好处是,您可以确定答案对于您的实施是正确的,并且您不会面临难以回答的问题的风险。

&(行向>计数)

-> has a higher priority than & (address of). - >的优先级高于&(地址)。 So your expression would be evalutated as &(row->count) 所以你的表达式将被评估为&(row-> count)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM