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

时间:2020-03-06 14:56:44  来源:igfitidea点击:

给定以下内容:

&row->count

将使用C ++评估&(row-> count)或者(&row)-> count吗?

编辑:这是C ++优先级的绝佳链接。

解决方案

这已经被问到了。但这是一个链接。

编辑:
好的,这个问题非常相似。可能还有另一个。

&(行->计数)

&(行->计数)

->具有比&(地址)高的优先级。因此,表情将被评估为&(row-> count)

此处说明C运算符的优越性

根据该表,->比&运算符具有更高的优先级,因此它是&(row-> count)

就优先规则而言,我一直很喜欢Steve Oualline在" Practical C"中提出的规则:

There are fifteen precedence rules in
  C (&& comes before || comes before
  ?:). The practical programmer reduces
  these to two:
  
  1) Multiplication and division come
  before addition and subtraction. 
  
  2) Put parentheses around everything
  else.

我可以建议我们使用测试程序解决此类问题吗?这样做的好处是,我们将肯定会知道答案对于实现是正确的,并且我们不会面临回答错误的风险。