在 C++ 中通过引用调用

发布于 2024-08-23 21:43:42 字数 162 浏览 7 评论 0原文

实际上通过引用函数在调用中传递了什么?

void foo(int &a,int &b)

当我写下

foo(p,q)

实际传递给函数的内容时。是p和q的地址吗?

What is actually passed in call by reference to a function?

void foo(int &a,int &b)

when I write

foo(p,q)

what is actually passed to the function. Is it the address of p and q?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

手心的海 2024-08-30 21:43:42

实际上传递给函数的是一个引用。命名参数b 成为参数对象q 的同义词。

编译器可能如何实现这一点:调用者在调用之前将 q 的地址放置在堆栈或寄存器中,并且被调用者使用该值来实现对 b 的所有访问。但将其描述为“实际传递”指针可能会产生误导,因为参数传递是 C++ 语言级别的概念,并且在该级别上它与传递指针不是同一概念。例如,当您传递指针时,您可以传递空指针,但当您传递引用时,您不能(有效)。所以说它们是同一件事是错误的。

也就是说,实现编译器的人可能会将其描述为“实际上传递一个指针”,并且您知道他们的意思。作为比较,如果 char 变量在调用约定中占用 4 字节堆栈槽,则它们可能会说编译器“实际上正在传递 int”。所以这取决于“实际上”的含义。

What's actually passed to the function is a reference. The named parameter b becomes a synonym for the argument object q.

How the compiler probably implements this that the caller places the address of q on the stack or in a register before calling, and the callee uses that value to effect all accesses to b. But it could be misleading to describe that as "actually passing" a pointer, because parameter passing is a concept at the level of the C++ language, and at that level it is not the same concept as passing a pointer. For instance, when you pass a pointer you can pass a null pointer, but when you pass a reference you cannot (validly). So it'd be wrong to say they're same thing.

That said, the person implementing the compiler might describe it as "actually passing a pointer", and you know what they mean. For comparison, if char variables occupy 4-byte stack slots in the calling convention, they might say that the compiler is "actually passing an int". So it depends what "actually" is supposed to mean.

时光沙漏 2024-08-30 21:43:42

其他答案提到了引用和指针之间的语义差异。

实际上,我使用过的每个编译器都以相同的方式实现它们——传递引用实际上是在汇编级别传递指针。这没有在任何标准中指定,只是各地实践中的情况。

这个问题之前就出现过: 指针和引用之间的低级区别是什么?

The other answers mention the semantic difference between a reference and a pointer.

In practice, every single compiler I've ever worked with implements them the same way -- passing a reference is really passing a pointer at the assembly level. This isn't specified in any standard, it's just the case in practice everywhere.

The question's come up on SO before: What's the low-level difference between a pointer an a reference?

海夕 2024-08-30 21:43:42

它确实传递了一个引用类型——它有点像地址,但又不完全一样。实际地址将是一个指针。引用不如指针强大,但可以说更安全。 维基百科对指针和指针之间的差异有很好的描述参考。

It really gets passed a reference type - it's sort of like the address, but not quite. The actual address would be a pointer. References are less powerful than pointers, but are arguably safer. Wikipedia has a good description of the differences between pointers and references.

2024-08-30 21:43:42

您传递的是一个引用,它不是指针,也不是地址——但它是相似的。

“确切”的参考是什么,并不是一成不变的。该标准没有规定处理引用的机制——只是规定使用它们的后果。 通常,它们将被实现为指针。

示例:

int foo(int& a, int& b) { a = b; }

// Usage
int x, y;
foo(x, y);

这可能会生成与以下内容相同的机器代码:

int foo(int* a, int* b) { *a = *b; }

// Usage
int x, y;
foo(&x, &y);

但不能保证这一点,并且两者并不等效(尽管它们提供类似的功能)。

当您获取引用的地址时,您将获得与其所引用的对象相同的地址。例子:

void foo(int& x) { std::cout << &x << std::endl; }

int y;
std::cout << &y << std::endl;
foo(); // This will print the same as above.

You are passing a reference, which is not a pointer, and not an address -- but it is similar.

What "exactly" a reference is, isn't set in stone. The standard does not dictate the mechanisms for dealing with reference -- just the consequences of using them. Usually, they will be implemented as pointers.

Example:

int foo(int& a, int& b) { a = b; }

// Usage
int x, y;
foo(x, y);

This might generate the same machine code as:

int foo(int* a, int* b) { *a = *b; }

// Usage
int x, y;
foo(&x, &y);

But there's no guarantee of that, and the two are NOT equivalent (although they provide similar functionality).

When you take the address of a reference, you get the same address as the object that it is referring to. Example:

void foo(int& x) { std::cout << &x << std::endl; }

int y;
std::cout << &y << std::endl;
foo(); // This will print the same as above.
趁年轻赶紧闹 2024-08-30 21:43:42

引用传递的工作方式由编译器定义。无论如何,它们都作为 int& 传递,这是 C++ 中的实际类型。试试这个:

int x = 10;
int& y = x;
x = 100;

你认为 y 的值是多少?引用并不完全是指针,而是变量的别名。编译器应用于 int& 的机制也用于按引用传递参数。

给定以下程序:

void byRef(int& x)
{
    return;
}

void byVal(int x)
{
    return;
}

void byPtr(int * x)
{
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{

    int x = 0;
    byRef(x);
    byVal(x);
    byPtr(&x);

    return 0;
}

byRefbyPtr 调用生成的 MSVC90 程序集完全相同,即:

lea eax, [x]
push eax
call byRef ;or byPtr
add esp, 4

The way pass by reference works is defined by your compiler. In any event, they are passed as int& which is an actual type in C++. Try this:

int x = 10;
int& y = x;
x = 100;

What do you think is the value of y? References are not quite pointers but aliases to a variable. The same mechanism that your compiler applies to int& is used for pass by reference parameters.

Given the following program:

void byRef(int& x)
{
    return;
}

void byVal(int x)
{
    return;
}

void byPtr(int * x)
{
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{

    int x = 0;
    byRef(x);
    byVal(x);
    byPtr(&x);

    return 0;
}

The MSVC90 Assembly generated for the byRef and for byPtr calls are exactly the same which is:

lea eax, [x]
push eax
call byRef ;or byPtr
add esp, 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文