search

Home  >  Q&A  >  body text

C++ 返回局部变量的常引用

将函数的局部变量返回给常引用类型,编译运行没有问题,输出也是正确的。这个常引用指向的对象在什么地方。

#include <iostream>
using namespace std;

int func()
{
    int a = 10;
    return a;
}

int main()
{
    int const& a = func();
    cout << a << endl;

    return 0;
}
阿神阿神2804 days ago613

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 13:56:28

    1. Without considering copy ellison, const int& a points not to a in func, but to a copy of a in func (because the return value of func is int instead of int&).
    2, local const & will extend the life cycle of the temporary object (pure rvalue) generated by func() until the end of the const & life cycle.
    So there won’t be any problems.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:56:28

    Generally speaking, functions can return local variables. The scope of local variables is only within the function. After the function returns, the memory of the local variables has been released. Therefore, if the function returns the value of a local variable and does not involve the address, the program will not error. But if the address (pointer) of a local variable is returned, an error will occur after the program is run. Because the function just copies the pointer and returns it, but the content pointed to by the pointer has been released, so the content pointed to by the pointer is unpredictable, and the call will go wrong.
    You can use VS, IDA, windbg and other tools for disassembly and debugging, and the process is clear.
    The above is to directly assign 10 to the a variable in main.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:56:28

    Returning a local object from a function via a C++ constant reference:

    It is generally incorrect to return a reference to a local object from a function:

    int & fun ( void )   
    {   
        int t = 3;   
        return t;   
    } 
    

    Special case: return a constant reference

    const int & fun ( void )   
    {   
        int t = 3;   
        return t;   
    }   
    const int & my_t = fun ();  
    

    In this case, the local variable t will not be destroyed directly, but will be retained until the end of the life cycle of my_t.

    In short, C++ constant reference syntax can refer to a temporary variable. This approach makes sense when using references as function parameters and returning local variables. It seems to me that constant references are mainly used as function parameters or to ensure that the original variables are not modified.

    There are two points worth noting about the initialization of references:

    (1) When the initialization value is an lvalue (the address can be obtained), there is no problem;

    (2) When the initialization value is not an lvalue, a const T& (constant reference) can only be assigned a value. And there is a process for this assignment:

    First implicitly convert the value to type T, then store the conversion result in a temporary object, and finally use this temporary object to initialize the reference variable. In this case, the temporary object used in the const T& (constant reference) procedure will coexist and die with the const T& (constant reference).

    Link description

    reply
    0
  • Cancelreply