C ++中的参考资料,它是如何工作的? [英] References in C++, how it really works?

查看:62
本文介绍了C ++中的参考资料,它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++中的引用有疑问。从我的理解,它就像一个指针,但有一些差异。它不能为空,不能重新分配,必须初始化。除此之外,它的行为几乎就像一个指针。但是,当我一直在网上寻找答案时,我已经看到了一些例子,这表明引用并不是真正的指针。



在示例中下面我预计函数中的引用'n1'为10,因为它是对main中变量'n1'的引用。如果它是参考,那么即使值发生变化,它仍然可以为1。它必须是某个地方的副本,因为显然1和10同时存在。对我而言,它更像是价值的副本而不是参考。



I have a question about references in C++. From what I understand it's like a pointer but with a few differences. It can't be null, it can't be re-assigned and it must be initialized. Apart from this it behaves pretty much as a pointer. However I have seen some examples when I have been searching the web to find answers, that showed that references doesn't really behave as pointers after all.

In the example below I expected the reference 'n1' in the function to be 10 since it's a reference to the variable 'n1' in main. If it's a reference how can it then still be 1 even if the value is changed. It must be a copy somewhere since obviously both 1 and 10 exists at the same time. For me it acts more like a copy of the value than a reference.

#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}
 
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}



输出:


Output:

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12





如果有人知道可以帮助我,我将感激不尽理解c ++中的引用到底是什么以及它是如何存储在内存中的。 (我不是新编程,但缺乏一些c ++的经验和知识)。



代码取自: http:// en.cppreference.com/w/cpp/utility/functional/ref [ ^ ]



问题解决了,我没有意识到绑定实际上取值,因此 n1 只是函数中的临时变量。我认为这种符号有点令人困惑。无论如何,谢谢大家的帮助!



I would be thankful if someone of you that know could help me understand what a reference in c++ really is and how it's stored in memory. (I'm not new to programming but lack some experience and knowledge in c++).

The code is taken from: http://en.cppreference.com/w/cpp/utility/functional/ref[^]

The problem is solved, I didn't realized that bind actually takes a value and therefore n1 is only a temporary variable in the function. The notation was a bit confusing though I think. Anyway thanks for all help!

推荐答案

好的,我想你可能会对这两件事感到困惑:



&和*



&并不意味着引用它意味着地址,它传递右侧项目的地址。与&传递时,值通过引用传递,这意味着您可以修改值,它们会反映在调用堆栈中。



*是解引用运算符,返回项的值。按值传递意味着您可以在函数中执行任何操作,但其值不会反映回调用堆栈(当函数退出时,值将恢复为原始值)。



*编辑*



我可能太快回答了,请参阅这个 [ ^ ]看看std :: ref和std :: cref真正做了什么以及它是如何工作的。



我看到你得到了代码来自此处 [ ^ ]
Ok, I think you may have confusion about these two things:

& and *

& does not mean "reference" it means Address Of, which passes the address of the item on the right side. When passed with &, the values are passed by reference which means you can modify the values and they are reflected up the call stack.

* is the dereference operator, which returns the value of the item. Passing by value means you can do whatever you want in the function but its value is not reflected back up the call stack (the value reverts back to the original when the function exits).

*EDIT*

I may have been too quick to answer, see this[^] to see what the std::ref and std::cref really do and how it works.

I see you got the code from here[^]


As bind 函数参数, n1 不是引用(它是按值传递的)因此 f 获取引用临时变量。
As bind function argument, n1 is not a reference (it is passed by value) hence f gets a reference of a temporary variable.


这篇关于C ++中的参考资料,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆