Summary of C + + references

Source: Internet
Author: User

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 (&AMP;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&AMP;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 (&AMP;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

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.