C ++ references

Source: Internet
Author: User

C ++ references

C ++ reference learning:

Generally, the first function of referencing is the alias of the referenced variable. (This variable can be directly operated );

 

Referenced statement:

Type + & + name (can be considered as a regular pointer)

Note: (1) & indicates the identifier;

(2)When declaring a reference, you must first initialize it;

   (3) You cannot create an array reference. Because an array is composed of several elements, you cannot create an array alias;

 

Referenced applications:

(Reference as a parameter)

int swap(int &a, int &b){    int t = a;    a = b;     b = t;} 

(Reference as a constant)

int main(){  int b = 100;  const int &a = b;  return 0;}

This makes the code robust and has other requirements;

 

(Reference as return value)

Type + & + function name

(1) to reference the returned function value. to define a function, add & before the function name &;

(2) The biggest benefit of returning a function value with a reference is that a copy of the returned value is not generated in the memory.

 

But when the function returns the stack variable,Cannot be the initial value of other references; (Because the stack variable will be released after the function is completed)

Int & fun () {return a; // when the local variable is released, the reference to the local variable will point to an unknown memory space} int main () {int & a = fun ();}
// This is incorrect

And cannot be left;

 

 

The function returns static or global variables: (both variables are placed in the global zone)

Can be referenced as other values, and can be used as the left and right values;

 

Write a code in the above situations:

// Return the static variable int & fun1 () {static int a = 1; return a ;}// return the local variable int & fun2 () {int a = 2; return a;} // The referenced int & fun3 (int & a) {return a;} int main () {int a = fun1 (); int & B = fun1 (); cout <"a =" <a <endl; cout <"B =" <B <endl ;}
/* The running result is:
A = 1;
B = 1;
*/

This is because the parameter is global;

 

Main () {int a = fun2 (); int & B = fun2 () ;}/ * The running result is 2 and-2 (negative number indicates that the point to memory is not clear )*/

This is because the reference is a local variable that points to an ambiguous memory zone when the 2nd semicolon ends;

 

Main (){
Int c = 10; int a = fun3 (c); int & B = fun3 (c);}/* The running result is a = 10; B = 10 ;*/

The reference at this time is because the internal program will not be released before it ends;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.