use*_*417 3 c++ parameters pointers reference memory-address
为什么“无效”实际上返回“ 6”?
void Void (int &ref){
ref++;
}
int main () {
int test= 5;
Void(test);
cout << test; // is 6
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不太了解这里发生了什么。使用Void(test)我没有通过测试的地址。为什么不使用“ Void(&test);”?为什么ref ++将1加到值5?不应该是“ * ref ++”吗?
int &ref = test;
Run Code Online (Sandbox Code Playgroud)
ref初始化为对变量的引用test。引用实质上是另一个对象的别名。尽管使用了熟悉的语法(即amersand &),但这并没有占用任何地址,从语义上讲,它与指针无关。