c++ - 使用带或不带括号的 C 符号

标签 c++ c operator-precedence ampersand addressof

让我们有两行代码:

&car->speed
&(car->speed)
  1. 这两行是否等价?在这两种情况下我都能得到 speed 的地址吗?
  2. 如果它们是等价的,那么选择什么作为编码约定更好?

最佳答案

Are these two lines equivalent? Will I get in both cases address to the speed?

是的。 -> 比一元 & 具有更高的优先级,因此 &car->speed&(car->speed) 是等价的。

If they are equivalents, what is better to choose as coding convention?

选择第二个,因为它显示了您对 speed 地址感兴趣的预期行为。

关于c++ - 使用带或不带括号的 C 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919241/

相关文章:

Perl:.=~是什么意思

c - 为什么这些构造使用增量前和增量后未定义的行为?

c++ - Mac OSX、Qt Creator 和 Google Test

c - 使用 atoi

c++ - 正确使用模板函数

c - 获取网关以用于 ANSI C 中的给定 ip

c - 尝试将文本文件读入数组而不在 C 中重复

c++ - 有没有允许重载运算符优先级的语言?

c++ - 自由函数和成员函数的自定义 std::function-like 实现

c++ - cudaMemcpy 段错误