我的问题非常直截了当,我相信可以理解。 当我通过引用传递值时,我做了这个简单的片段来说明我的冲突。
int main() {
int a = 1;
int &b = a;
}
我知道这是正确的做法,但是获取 b 的地址并使其等于 a 的值有何意义。逻辑上应该是:int &b = &a;
最佳答案
许多运算符是上下文相关的。 &
“运算符”根据上下文可能表示三种不同的含义:
可以在定义引用时使用
int& r = a; // The variable r is a reference of the variable a
它可以用来获取指向某物的指针
int* p = &a; // Here & is the address-of operator which returns a pointer // Here it makes p point to the variable a
可能是 bitwise AND运算符
0x53 & 0x0f // Here's a bitwise AND operation, the result is 0x03
关于c++ - 通过引用符号传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54986132/