简体   繁体   English

在C ++中存储引用变量

[英]store reference variable in c++

I have been struggling hard to find the difference between the following two pieces of code. 我一直在努力寻找以下两段代码之间的区别。

This ... 这个 ...

int z=10;
int y=&z;

... is invalid whereas the following does not throw any error: ...无效,但以下内容不会引发任何错误:

int& foo()
{
    int z=10;
    return z;
}
main()
{
    int y=foo();
    cout<<y;
    return 0;
}

When I tried to run the program it returned y=10 . 当我尝试运行该程序时,它返回y=10

My question is: 我的问题是:

If y can store the reference of another variable using foo() , why not directly using y=&z ? 如果y可以使用foo()存储另一个变量的引用,为什么不直接使用y=&z

 int y=&z;

The ampersand above does not indicate y is a reference, instead you're applying the address of operator to z , meaning you're taking its address. 上面的&符号并不表示y是引用,而是将运算符的地址应用于z ,这意味着您要获取其地址。

 int& y=z;

Here y is a reference to z . 此处y是对z的引用。


In your second example you have undefined behavior because the function foo() returns a reference to a variable that is local to the function. 在第二个示例中,您具有未定义的行为,因为函数foo()返回对该函数本地变量的引用。 The storage for z within foo() will cease to exist when the function returns, and then accessing it via the returned reference is undefined behavior. 函数返回时,在foo() z的存储将不再存在,然后通过返回的引用对其进行访问是未定义的行为。

If you want to create reference variable do 如果要创建参考变量,请执行

int z=10;
int& y=z;
//int y = &z; this is not correct.

y store values, not references, because y is defined int y instead of int& y . y存储值,而不是引用,因为y定义为int y而不是int& y Because of this foo( ) returns a reference, but y stores the value of the reference... that is 10 . 因此, foo( )返回一个引用,但是y存储该引用的值...为10

Next code will fail: 下一条代码将失败:

int& foo()
{
    int z=10;
    return z;
}
main()
{
    int& y = foo(); // <-- now y is a reference
    cout<<y;        // <-- at this point z does not exists
    return 0;
}

The problem is with this code: 问题在于此代码:

int& foo()
{
    int z=10;
    return z;
}

z is an internal variable whose scope is with-in foo function. z是一个内部变量,范围为with-in foo函数。 As soon as function body ends, z is no more in the memory. 一旦函数体结束,z将不再在内存中。 But we have passed the reference of internal variable to the outer world (which is out of its scope). 但是我们已经将内部变量的引用传递给了外部世界(这超出了它的范围)。

int z=10; // create integer z and set it to the value 10.
int y=&z; // create integer y and set it to the address of z 

The code above doesn't have anything to do with references.... 上面的代码与引用没有任何关系。

int &y = z; // create a reference (y) to integer z

Is more like what you want. 更像是您想要的。

In C++, a reference variable is similar to an alias (as-if having different name for accessing the same variable). 在C ++中,引用变量类似于别名(假设访问相同变量的名称不同)。 As such, 因此,

int z=10;
int &y=z; // y is a reference variable, and shares same space of z
y++; // this makes z++ effectively, i.e. z becomes 11

and

int y = &z; // your example is wrong, as rvalue is int * and lvalue is int

Now, when you have, 现在,当你有

int& foo()
{
    int z=10;
    return z;
}
main()
{
    int y=foo();
    cout<<y;
    return 0;
}

This is dangerous, as you are returning reference of a local variable (z) which is going to be destroyed upon returning from foo. 这很危险,因为您返回的是局部变量(z)的引用,该变量在从foo返回时将被销毁。 This should have been used like returning a value (instead of reference), as local variables go out of scope upon return from function. 这应该像返回值(而不是引用)一样使用,因为局部变量在从函数返回时会超出范围。 Thus, correct usage is: 因此,正确的用法是:

int foo() // return by value
{
    int z=10;
    return z;
}
main()
{
    int y=foo();
    cout<<y;
    return 0;
}

Hope this helps. 希望这可以帮助。 Advise is that never ever return reference of a local variable. 忠告是,永远不要返回局部变量的引用。

When you use the function, you're simply indicating that the function doesn't push the value on the stack, it instead returns the value by reference. 使用该函数时,仅表示该函数没有将值压入堆栈,而是通过引用返回该值。 Somewhat pointless for a simple int, but more useful if you're returning a struct or class. 对于简单的int来说毫无意义,但如果返回结构或类,则将更为有用。

In the y=&z case, you're attempting to set an int equal to an address. y=&z情况下,您尝试将int设置为等于地址。

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

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