Reference to do function parameters
structteacher{Charname[ -];intAge;};voidPrintft (Teacher *pt) {cout<<pt->age<<endl;}//pt is the alias of T1, which is equivalent to modifying the T1voidPrintfT2 (Teacher &pt) {//cout<<pt.age<<endl;Pt.age = -;}//pt and T1 are two different variables.voidPrintfT3 (Teacher PT) {cout<<pT.age<<endl; Pt.age = $;//Only the PT variable is modified and the T1 variable is not modified}voidMain () {Teacher T1; T1.age = *; Printft (&T1); PrintfT2 (t1);//pt is T1 's nickname. printf("t1.age:%d \ n", t1.age);//33PrintfT3 (t1);//PT is formal parameter, T1 copy a copy of the data to PT//---> pt = T1 printf("t1.age:%d \ n", t1.age);//35 cout<<"Hello ..."<<endl; System"Pause");return;}
The nature of the reference
struct Teacer { int//4个字节,类似指针 int &b;};int main(){ printf("sizeof(Teacher) %d\n"sizeof(Teacer)); system("pause"); return0;}
1) The internal implementation of the reference in C + + is a constant pointer to the type& name ==type* const name
2) The C + + compiler uses a 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 perspective of use, the reference will make people misunderstand that it is just an alias, not their own storage space. This is the hidden detail of C + + for practicality. When an argument is passed to a formal parameter reference, it is simply a C + + compiler that manually takes an argument address and passes it to the formal parameter reference (constant pointer)
The function return value is a reference
int getAA1 () {inta; a=Ten; return a;}//base TypeaWhen returned, there will also be a copy of int& getAA2 () {inta; a=Ten; return a;}int* getAA3 () {inta; a=Ten; return&a;}
Pointer reference
#include <iostream>using namespace STD;namespaceNamespaceA {intA = -;}namespaceNAMESPACEB {intA =Ten;namespaceNAMESPACEC {//Namespace nesting structTeacher {stringNameintAge }; }}structTeacher {stringNameintAge;};voidOperatorteacher (Teacher *ConstP) {//pointers cannot be modified}voidOperatorteacher (ConstTeacher * p) {//The data pointed to by the pointer cannot be modified}voidSwapint&a,int&B) {//reference Exchange, C + + compiler for constant pointer implementation, * const intC c = A; A = b; b = C;}voidGetteacher (Teacher **p) {//*p=ptTeacher *tmp = NULL; TMP = (Teacher *)malloc(sizeof(Teacher)); Tmp->age = -; *P = tmp;}voidGetTeacher2 (teacher* &p) {//p is the PT aliasTeacher *tmp = NULL; TMP = (teacher*)malloc(sizeof(Teacher)); Tmp->age = -; p = tmp;}intMain () {//using namespace Namespaceb::namespacec; cout<<"Hello"<< Endl;//cout << namespaceb::a << Endl; intA =0;int&b = A;//reference, B is a alias Const int&c = A;//constant reference, cannot change the value //cout << b << Endl; intx =1, y =2; Swap (x, y);//cout << x << endl << y << Endl;Teacher *pt = NULL; Getteacher (&PT);//Get data by pointer, generate data in sub-functionGetTeacher2 (PT);//Fetch data by reference, generate data within the sub-function cout<< pt->age << Endl;return 0;}
Frequently cited
voidMain () {//General reference intA =Ten;int&b = A;//Constant reference: Let the variable refer to a read-only property Const int&c = A;///constant reference initialization is divided into two types //1 initialization of constant references with variables{intx = -;Const int& y = x;printf("y:%d \ n", y); }//2 to initialize constant references with constants{//int &m = 10;//reference is a memory space alias literal 10 no memory space no way to do reference Const int&m =Ten;//When a const reference is initialized with a constant (literal), the C + + compiler allocates space for the constant value and uses the reference name as the alias for the space}cout<<"Hello ..."<<endl; System"Pause");return;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Summary of C + + references