[英]Why we have to pass the values instead of the address into a “call by reference” function in C++?
I'm new to C++ and learning call by reference now. 我是C ++的新手,现在开始通过引用学习调用。
I learned this block of code on my material: 我在资料中学习了以下代码块:
void callByRef(&a, &b);
int main(){
int k = 12;
int m = 4;
callByRef(k, m) //QUESTION HERE
return 0;
}
void callByRef(&a, &b){
a += 3;
b -= 2;
return;
}
So my question is when declaring callByRef, we use the addresses of a and b as the parameters, so when we call callByRef in the main, why we need to pass in (k, m) instead of (&k, &m)? 所以我的问题是,在声明callByRef时,我们使用a和b的地址作为参数,因此当我们在主体中调用callByRef时,为什么需要传递(k,m)而不是(&k,&m)?
It's basic, but I just want to get the right picture for my future studying. 这是基本的,但是我只是想为将来的学习提供正确的画面。 Thanks guys!
多谢你们!
The operator of &a
means the address of variable a, the operator of *a
means the content of memory addressed by variable a. &a
的运算符表示变量a的地址, *a
的运算符表示变量a寻址的存储器的内容。 Now the function definition is following: 现在,函数定义如下:
void callByRef(int &a, int &b){
a += 3;
b -= 2;
return;
}
where int &a
means that a will be a reference to some variable of type int. 其中
int &a
表示a将是对 int类型的某些变量的引用 。 Therefore whenever you gong to call the function you need to pass the variable itself rather its address, hence callByRef(a, b)
and not callByRef(&a, &b)
. 因此,每当您要调用该函数时,都需要传递变量本身而不是其地址,即
callByRef(a, b)
而不是callByRef(&a, &b)
。
The reason of such declaration is following, parameters passed to the function, passed by value, namely the content of the variable literally copied into function variable, therefore any change to it inside the function won't have any effect. 这样声明的原因如下:传递给函数的参数,传递给值的变量,即按字面意义复制到函数变量中的变量的内容,因此,在函数内部对其进行的任何更改均无效。 For example:
例如:
void callByVal(int a, int b){
a += 3;
b -= 2;
return;
}
and having: 并且具有:
x = 5;
y = 10;
calling callByVal(x, y)
won't have any effect on x and y. 调用
callByVal(x, y)
对x和y无效。 To overcome this, you need to pass parameters either by reference or int &a
or as an alternative you can pass pointer and update memory directly: 为了克服这个问题,您需要通过引用或
int &a
传递参数,或者作为替代方案,您可以传递指针并直接更新内存:
void callByPtr(int *a, int *b){
*a += 3;
*b -= 2;
return;
}
And now it make sense to pass variables addresses, eg callByPtr(&a, &b)
. 现在可以传递变量地址了,例如
callByPtr(&a, &b)
。
You don't pass the value. 您不会传递值。 References in C++ aren't exactly like pointers.
C ++中的引用与指针不完全一样。 Think of them as simply being another name for an object.
可以将它们视为对象的另一个名称。
Naturally you need to bind that name to an object in order for the reference to refer to it. 自然,您需要将该名称绑定到对象,以便引用引用该对象。 So for instance when we write
int &i = k;
例如,当我们写
int &i = k;
we are saying that the variable that is already named k
, may be referred to as i
. 我们是说已经命名为
k
的变量可以称为i
。 To bind the reference, we need to name the other object. 要绑定引用,我们需要命名另一个对象。 And the name of the object is
k
, not &k
. 对象的名称是
k
,而不是&k
。
The same applies to function parameters and arguments. 函数参数和参数也是如此。 The function
callByRef
is specified to operate on two objects it will call a
and b
. 函数
callByRef
被指定为对两个对象进行操作,它将调用a
和b
。 When you call that function, you must name the original objects it will refer to. 调用该函数时,必须命名它将引用的原始对象。 And the original objects are named
k
and m
, not &k
and &m
. 并且原始对象被命名为
k
和m
,而不是&k
和&m
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.