通过引用符号传递
Pass by reference notation
我的问题很直白,相信可以理解。
当我通过引用传递值时,我做了这个简单的片段来说明我的冲突。
int main() {
int a = 1;
int &b = a;
}
我知道这是正确的方法,但是获取 b 的地址并使其等于 a 的值有何意义?逻辑上应该是:int &b = &a;
你的语法有误。这很不高兴,但是 &
既是运算符 的地址,又是 表示 "referente to".
的标记
int &b = a;
这将变量 b
声明为 int&
类型(对 int 的引用)并初始化为 a
。没有任何地址。
许多运算符是上下文相关的。根据上下文,&
"operator" 可能表示三种不同的含义:
定义引用时可以使用
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
我的问题很直白,相信可以理解。 当我通过引用传递值时,我做了这个简单的片段来说明我的冲突。
int main() {
int a = 1;
int &b = a;
}
我知道这是正确的方法,但是获取 b 的地址并使其等于 a 的值有何意义?逻辑上应该是:int &b = &a;
你的语法有误。这很不高兴,但是 &
既是运算符 的地址,又是 表示 "referente to".
int &b = a;
这将变量 b
声明为 int&
类型(对 int 的引用)并初始化为 a
。没有任何地址。
许多运算符是上下文相关的。根据上下文,&
"operator" 可能表示三种不同的含义:
定义引用时可以使用
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