c++ learning 11 -- reference return value

#include <iostream>
using namespace std;


// Referring to a local variable, the operation is an illegal space, and the result is unknown. 
int & fun()
{
    int a = 12;
    return a;
}

intmain ()
{
    int &b = fun();   // After the statement is executed, the variable a in the function will be released. 
    cout << b << endl; // so b now refers to an illegal space. 

    system( " pause " );
     return  0 ;
}

#if 0

Difference between reference and pointer:
1. Reference declarations must be initialized, but pointers do not.
2. After the reference is initialized, it cannot refer to other spaces, and the pointer can point to other spaces.
3. References do not occupy storage space, but pointers occupy space.
4. The reference efficiency is higher, and the pointer is an indirect operation.
5. The reference is safer, and the pointer can be offset.
 * Pointers are more flexible, operate addresses directly, and are more common to c and c++ .

The three functions of the & symbol:
 1. When declaring a variable, use & to refer to it.
2. Add & in front of the variable to indicate the address.
3 , 7 & 8 , which represent the bitwise AND operation.

#endif

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324953932&siteId=291194637