我遇到了这些代码,无法理解它们的杰出之处 意义。
如果通过引用传递,我知道在参数中使用*,但是 将被转移到(1.)中的*&和(2.)中的&。
我是菜鸟..
//(1.)
#include <iostream>
using namespace std;
int gobal_var = 42;
// function to change Reference to pointer value
void changeReferenceValue(int*& pp) //*& What is it ??
{
pp = &gobal_var;
}
int main()
{
int var = 23;
int* ptr_to_var = &var;
cout << "Passing a Reference to a pointer to function" << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
changeReferenceValue(ptr_to_var);
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}
//(2.)
#include <iostream>
using namespace std;
void fun(int &x) {
x = 20;
}
int main() {
int x = 10;
fun(x);
cout<< "New value of x is " <<x ;
return 0;
}