简体   繁体   English

C ++参数传递

[英]C++ argument passing

Okay, so I just started learning C++ and I'm finding the ins and outs of argument passing to be a little confusing. 好的,所以我刚开始学习C ++,并且发现参数传递的来龙去脉有点令人困惑。 I came across the following line in my C++ book; 我在C ++书中碰到了以下内容;

In C++, passing by reference is accomplished in two ways: using pointers and using references. 在C ++中,按引用传递是通过两种方式完成的:使用指针和使用引用。

It then goes on to show a program that switches the values of two int values, x and y (shown below). 然后继续显示一个程序,该程序切换两个int值(x和y)的值(如下所示)。 I was wondering, are the arguments to the swap() function really passed by reference in this case? 我想知道,在这种情况下, swap()函数的参数是否真的通过引用传递了? I thought they were being passed by address, which I've been told is just a special case of pass by value. 我以为他们是通过地址传递的,有人告诉我这只是通过值传递的一种特殊情况。

//Listing 9.6 Demonstrates passing by reference 

#include <iostream.h>

void swap(int *x, int *y);

int main()
{
    int x = 5, y = 10;

    cout << "Main.  Before swap, x:  " << x
         << " y: " << y << "\n";
    swap(&x,&y);
    cout << "Main. After swap, x:  " << x
         << " y: " << y << "\n";
    return 0;
}
void swap(int *px, int *py) 
{
    int temp;

    cout << "Swap.  Before swap, *px: "
         << *px << " *py: " << *py << "\n";

    temp = *px;
    *px = *py;
    *py = temp;

    cout << "Swap. After swap, *px: " << *px
         << " *py: " << *py << "\n";

}

Nice conflation of abstraction levels. 很好地融合了抽象级别。

Lowest level: when you pass a C++ pointer by value the pointer value is copied. 最低级别:通过值传递C ++指针时,将复制指针值。

Higher level: usually the pointer points to something, and that something is logically passed by reference. 更高级别:通常指针指向某个对象,并且该对象在逻辑上是通过引用传递的。

The most direct way to pass by reference in C++, is to use a reference argument. 在C ++中通过引用传递的最直接方法是使用引用参数。 Under the hood a C++ reference can (in some given case) be a pointer, but there's no way to access or determine anything about that pointer value. 在某种程度上,C ++引用可以(在某些情况下)是一个指针,但是无法访问或确定有关该指针值的任何内容。 So it doesn't make sense to say that the reference is passed by value or copied, and instead we talk about binding the reference or binding to the reference. 因此,说引用是按值传递或复制是没有意义的,相反,我们谈论的是绑定引用或绑定到引用。

In a correct program a reference can't be a null-reference, and this is a distinct advantage for references as formal argument type. 在正确的程序中,引用不能为空引用,这对于作为形式参数类型的引用来说是一个明显的优势。

Another advantage is that a reference to const can be bound to a temporary, so that for example 另一个优点是可以const的引用绑定到临时对象,例如

void foo( const std::string& s ) ...

can be called like 可以这样称呼

foo( "Blah!" )

which isn't possible when instead of a reference one uses a pointer to “implement” logical pass-by-reference. 当使用引用代替“实现”逻辑传递引用的指针时,这是不可能的。

You are right. 你是对的。 The function 功能

void swap(int *x, int *y);

is taking its arguments by-value . 正在按值接受参数。 Because the arguments are pointers, it can dereference them and thus modify the memory they point to. 因为参数是指针,所以它可以取消引用它们,从而修改它们指向的内存。 The pointers themselves (the addresses) are passed by-value, nevertheless. 尽管如此,指针本身(地址)还是按值传递的。 You can see that if swap assigns a different value (address) to one of its arguments, the change isn't reflected in the caller. 您可以看到,如果swap为其参数之一分配了不同的值(地址),则该更改不会反映在调用方中。

int a = 1;
int b = 2;
int * pa = &a;
int * pb = &b;
swap(pa, pb);
assert((pa == &a) && (pb == &b));

Try adding a stuff like 尝试添加类似的内容

px += 12;
py += 17;

at the end of swap . swap结束时。 The assert ion will still hold. assert仍将保持。

In C++, a pointer is an object in its own right. 在C ++中,指针本身就是一个对象。 It has a value (the address it points to) and can be copied or assigned a different value. 它有一个值(它指向的地址),可以复制或分配一个不同的值。 It can also be passed by-reference or by-value just like other objects. 就像其他对象一样,它也可以按引用或按值传递。

A function that would take a pointer by-reference would look like this. 可以通过指针引用的函数看起来像这样。

#include <iostream>

void
pointer_by_reference(int * & p)
{
  p = nullptr;
}

int
main()
{
  int a;
  int * pa = &a;
  pointer_by_reference(pa);
  std::cout << "&a = " << &a << ", pa = " << pa << "\n";
}

Possible output: 可能的输出:

&a = 0x7ffcfdf2e00c, pa = 0

As you can see, pointer_by_reference has changed the value of pa . 如您所见, pointer_by_reference更改了pa的值。

Here its Call by Address as you are catching address of variables. 当您捕获变量的地址时 ,此处按地址调用 In case of Call by Ref we uses void swap(int &x, int &y); 如果通过引用调用,我们使用void swap(int &x, int &y); as prototype. 作为原型。

Details check Diff Between Call By Reference And Call By Pointer 详细信息检查按引用调用和按指针调用之间的差异

also see for knowledge What are the differences between a pointer variable and a reference variable in C++? 还可以参考了解知识,C ++中的指针变量和引用变量之间有什么区别?

There are basically two techniques of parameter passing. 基本上有两种参数传递技术。

  1. Call-by-value 按值致电
  2. Call-by-reference (also known as call-by-address) 按引用呼叫(也称为按地址呼叫)

Your code is indeed passing parameters by reference. 您的代码确实是通过引用传递参数。

With a call-by-value, when data is passed to a parameter in a procedure, only the value is made available to the procedure's parameter. 使用按值调用时,将数据传递给过程中的参数时,仅该值可用于过程的参数。 So any change you make to the parameter within the procedure has no effect on the calling routine's variable. 因此,对过程中的参数所做的任何更改都不会影响调用例程的变量。

But with a call-by-reference, the address of the variable is made available to the parameter. 但是通过引用调用,变量的地址可用于参数。 In other words, the parameter is actually pointing to the same location as the variable. 换句话说,参数实际上指向与变量相同的位置。 So changing the parameter within the procedure actually changes the variable passed to it. 因此,在过程中更改参数实际上会更改传递给它的变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM