简体   繁体   English

为什么我们必须将值而不是地址传递给C ++中的“按引用调用”函数?

[英]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)

The callByRef() function passes the references as arguments so you don't need to send the explicit reference to the function. callByRef()函数将引用作为参数传递,因此您无需将显式引用发送给该函数。 Have a read through this to understand a bit better. 通读此书可以更好地理解。

Tutorial 讲解

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被指定为对两个对象进行操作,它将调用ab 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 . 并且原始对象被命名为km ,而不是&k&m

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们通过引用传递时,为什么在调用函数时我们放置一个变量而不是地址(指针)? - When we pass by reference, why do we put a variable instead of address(pointer), when calling the function? 为什么 C++ std::function 通过值而不是通用引用传递函子? - Why C++ std::function pass the functor by value instead of universal reference? 为什么在C ++代码中获取地址而不是值? - Why I get address instead of values , in C++ code? 在C ++中,为什么我们在重载函数中将引用参数声明为const? - in c++ why we declare reference parameter as const in overloaded function? C ++没有匹配函数可解决调用错误(默认通过引用传递) - C++ no matching function for call error (defaulting to pass by reference) c++ 中的向量 STL 是否通过引用/地址传递? - are vectors STL in c++ pass by reference/address? 在将派生类对象传递给C ++中具有基类参数的函数时,为什么要传递引用而不是值? - Why should you pass a reference instead of a value when passing a derived class object to a function with a base class parameter in C++? C++ 函数调用引用 - C++ Function call by reference 我遇到了一些C ++代码,为什么我们必须在块中使用* this而不是this? - I have come across some C++ code.Why we have to use *this in block instead of this? C ++:为什么我的函数返回的参考地址与解除引用的指针的地址不同? - C++: Why does my function return a reference address different from the dereferenced pointer's address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM