Examples of the nature of references in C + + and the use of reference as function parameters _c language

Source: Internet
Author: User
Tags numeric value

The meaning and essence of reference
1 refers to the existence of aliases as other variables, so it can be substituted for pointers in some situations
2 reference is more readable and practical than the pointer

To quote the essence of thinking:
Thinking, what does the C + + compiler do behind your back?

#include <iostream> 
using namespace std; 
 
int main () 
{ 
  int a = ten; 
  A separately defined reference must be initialized; The description is much like a constant 
  int &b = A; 
  B is the alias of a 
  B = one; 
  cout << "B--->" << a << Endl; 
  printf ("a:%d\n", a); 
  printf ("b:%d\n", b);  
  printf ("&a:%d\n", &a); 
  printf ("&b:%d\n", &b); 
  System ("pause"); 
  return 0; 
} 

A reference is an address, and a reference is a constant.

Char *const P

The nature of the reference:
1 The internal implementation of reference in C + + is a constant pointer
type& name <--> type*const name
2 the C + + compiler uses the constant pointer as the internal implementation of the reference during compilation, so the reference occupies the same amount of space as the pointer.
3 from the use of the point of view, the reference will make people misunderstand that it is only an alias, do not have their own storage space. This is the details of C + + for practicality to hide

Three conditions for the establishment of indirect assignment:
1 defines two variables (one argument with one parameter)
2 establishing the associated real parameter address to the formal parameter
3*p parameter to modify the value of the argument indirectly

The reference in the implementation, but is: indirectly assigned to the three conditions of the latter two steps and two for one.
When the argument is passed to the parameter reference, it is just the C + + compiler that helped our programmer to manually take an argument address and pass it to the parameter reference (constant pointer).

Reference to do function arguments

A generic reference must be initialized with a different variable when it is declared.
The reference is not initialized as a function parameter declaration

References to complex data types 
#include <iostream> 
using namespace std; 
 
struct Teacher 
{ 
  char name[64]; 
  int age; 
}; 
 
void Printft (Teacher *pt) 
{ 
  cout << pt->age << endl; 
} 
 
PT is the alias of T1, which is equivalent to modifying the T1 
void printfT2 (Teacher &pt) 
{ 
  //cout<<pt.age<<endl; 
  Pt.age = n; 
} 
 
PT and T1 are two different variables of 
void PrintfT3 (Teacher pt) 
{ 
  cout << pt.age << Endl; 
  Pt.age = 45; Only the PT variable is modified and the T1 variable 
} 
void Main () 
{ 
  Teacher T1) is not modified; 
  T1.age =; 
 
  Printft (&T1); 
 
  PrintfT2 (t1); PT is T1 alias 
  printf ("t1.age:%d \", t1.age);//33 
 
  printfT3 (t1);//PT is formal parameter, T1 copy a data to PT   //---> pt = t1< c33/>printf ("t1.age:%d \", t1.age); 
 
  cout << "Hello ..." << Endl; 
  System ("pause"); 
  return; 
} 


Difficult to reference: function return value is reference (reference is left value)
when the function return value is a reference, if the stack variable is returned, it cannot be the initial value of other references and cannot be used as the left value.
If you return a static or global variable, you can become an initial value for another reference, either as a right value or as a left value.
In C + + chained programming, references are often used.

#include <iostream> 
using namespace std; 
The return value is the underlying type when the reference 
int getAA1 () 
{ 
  int A; 
  A = ten; 
  return A; 
} 
 
When the underlying type a returns, there will also be a copy 
int& getAA2 () 
{ 
  int A;//if the reference on the stack is returned, there may be a problem 
  a = ten; 
  return A; 
} 
 
int* getAA3 () 
{ 
  int A; 
  A = ten; 
  Return &a; 
} 
 
int main () 
{ 
  int a1 = 0; 
  int a2 = 0; 
 
  A1 = GetAA1 (); 
  A2 = GetAA2 (); A is an 
  int &a3 = getAA2 ();//If the stack variable is returned, it cannot be the initial value of other references 
  cout << A1 << Endl; 
  cout << A2 << Endl; 
  cout << A3 << Endl; A3 is garbled, here appears the problem 
  //compiler see A3 is a reference, automatically to A3 address to take the value 
  //But the function GetAA2 exit has released the memory of this address, so here is garbled return 
 
  0; 
} 

The return value is a static variable when the reference

When static modifies a variable, the variable is a state variable 
int J () 
{ 
  static int a = ten; 
  a++; 
  printf ("a:%d \ n", a); 
  return A; 
 
} 
 
int& J1 () 
{ 
  static int a = ten; 
  a++; 
  printf ("a:%d \ n", a); 
  return A; 
} 
 
int *j2 () 
{ 
  static int a = ten; 
  a++; 
  printf ("a:%d \ n", a); 
  Return &a; 
} 
 
 
The result of void main () 
{ 
  //J () is a numeric value with no memory address and cannot be 
  //11 = n; 
  * (a>b?&a:&b) = A; 
  When the called function is left, it must return a reference 
  J1 () = 100;//compiler helps us create environment 
  J1 (); 
  * (J2 ()) = 200; The equivalent of manual build, do the conditions of the left 
  J2 (); 
  System ("pause"); 
} 

The return value is a formal parameter when the reference

int g1 (int *p) 
{ 
  *p = m; 
  return *p; 
} 
 
int& G2 (int *p)// 
{ 
  *p = m; 
  return *p; 
} 

When using the reference syntax, do not care about what the compiler references do 
//When parsing garbled this phenomenon, to consider the C + + compiler is how to do .... 
void Main () 
{ 
  int a1 = ten; 
  A1 = G2 (&A1); 
 
  int &a2 = g2 (&A1); Use a reference to accept the return value of the function, is not garbled, the key is to see whether the return of memory space by the compiler to recycle .... 
  printf ("a1:%d \", A1); 
  printf ("a2:%d \", a2); 
 
  System ("pause"); 
} 

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.