C + + (often) reference, const, pointer reference __c++

Source: Internet
Author: User
Tags advantage

First, the reference
1. Basic Reference syntax

/* Variable reference *
/int i = ten;
int & s = i;//declaration refers to the variable s, which refers to I, then subsequent changes to s or I will cause the value of s and I to change. At this point s and I equivalence
//Declaration of a reference, not a new definition of a variable, it only means that the reference name is an alias of the target variable name, it is not a data type itself, so the reference itself does not occupy the storage unit, the system does not allocate the storage unit to the reference. Therefore: the reference to the address, that is, the target variable to find the address. &s and &i are equal. thereby reducing memory space and improving operational efficiency. That's the best thing to quote.

2. Reference as a function parameter, the main advantage here is that when making the main function call, you do not need to assign a value to the parameter, allocate space, but directly to the actual parameter operation. This saves space and improves efficiency.
Although pointers can achieve such effects, you still need to assign a pointer variable to occupy the memory space.

Defines the swap function, a,b as a reference function parameter
void swap (int &a, int &b)
{
int c = A;
A = b;
b = C;
}
Defines the main function
void main ()
{
int m, n;
cin>>m>>n;
Swap (m,n);//call directly, without real parametric any requirements. PS, there is doubt, not to quote no special requirements ...
cout<<m<< ' <<n<<endl;
}

3 frequently cited
Syntax: const int &a = s;//can not modify a to change the value of s, but can change S.

int main ()
{
    int a =;
    int const &S = a;//here the const int &s =a effect is equivalent.
    cout << a << Endl;
    cout << s << endl;
    A =;
    s = 40;//Here cannot assign value ... Because S is often quoted
    cout << a << Endl;
    cout << s << endl;
    return 0;
}

4. Reference as the return value of the function, the biggest advantage is that the memory is less, because there is no copy of the return value.
Case:

Reference as a function return value, the biggest advantage is not to occupy memory (memory does not occupy a copy of the return value)

int swap1 (int a, int b)
{
    int temp = A;
    A = b;
    b = temp;
    cout << "Swap1" << Endl;
    return a/b;
}

Reference as function return value
float &swap2 (int a, int b) {
    int temp = A;
    A = b;
    b = temp;
    float c = (a*1.0)/b;
    cout << "Swap2" << Endl;
    return  C;
}

int main ()
{
    int a = SWAP1 (2, 3);
    Float B = SWAP2 (2, 3);
    cout << a << Endl;
    cout << b << Endl;  int &c = SWAP1 (2, 3);//Here is wrong, cannot refer to c
    float &d = SWAP2 (2, 3);  cout << c << Endl;
    cout << D << Endl;

    return 0;
}
Distinguish between several references:
t*;t* const; const t*; const t* const; t&; t& const; const t&; Const t& const;const t* &;t*const &

Here is a memory tip: The const is close to which one cannot be changed.

Regular pointers and frequently referenced #include "stdio.h" #include "iostream" using namespace std; int main () {//t*;t* const; const t*; const t* const; t&; t& const; const t&; Const t& Const;const t*, &A
    Mp;;t*const &/* pointer and constant pointer/int a = 18;
    int B = 20; int *p=&a;//denotes t type of pointer p int * Const p1=&a;//here must be assigned value because it is a constant pointer cout << p1 << endl;//p1=00000000,64 bit
    Translator *P1 = 11;
cout << *p1 << Endl;


P1 = &b;//Error///From above you can see that the int *const pointer is a regular pointer, he can only point to a fixed place, modify the number of the fixed place, but can not point to other places.
    const int *P2;
    int const *p2;//Both of these definitions can be p2 = &a;
    cout << p2 << Endl;
    P2 = &b;
cout << p2 << Endl;

    *P2 = 25;//Here is wrong, can not be assigned to P2/by the above, the const int *P2 indicates that the pointer points to a constant, where the value cannot be changed, but the pointer can point to another place.
    const INT * Const P3=&AMP;A;//here must be directly assigned//P3 = &a;//error, equivalent to both of the above cases including cout << P3 << Endl;
cout << *p3 << Endl;
    P3 = &b;//error, where P3 is a regular pointer, he can only point to a fixed place, and this fixed place is also a constant, can not be changed. Cout << P3 << Endl;



cout << *p3 << Endl;
    /* Reference and constant reference/int &c = a;//reference C and a equivalence, can change each other c = 100;
    cout << c << Endl;
    cout << a << Endl;
    A = 200;
    cout << a << Endl;

cout << c << Endl; int & Const D = a;//personally feel the const here is nothing to use AH.
    equivalent to int &d=a; d = 300;//a and D are both cout << D << Endl;
    cout << a << Endl;
    A = 500;


    cout << D << endl;//d at this time became cout << a << Endl;
    const int & e = a;//is equivalent to int const &AMP;E=A//E = 400;//error, where E is a constant reference, you cannot change a = 400;//You cannot change the value of E, but you can change the value of a to change the value of E.
    cout << e << Endl;

    cout << a << Endl;
    The const int & Const F=A;//F cannot be assigned a value, but a can be assigned a value of cout << f << Endl;
cout << a << Endl;
    F = 1000;//cannot be assigned to F; F is a constant.
    cout << F << Endl;
    cout << a << Endl;
    A = 2000;
    cout << F << Endl; cout << a << Endl; To sum up, we found that, even if the reference can not change the value of references, but the value of the variable may be changed by changing the value of the commonly referenced.


It is not understood here why the constant reference should be a constant and can be changed.
    Const t*, as you can see here, the const is in front of the *, indicating that the pointer to the content cannot be changed, but the pointer can point to another place const int *p10=&a;
    const INT *p11=&b; const int * &AMP;P12 = p10;//p12 p10 references cout << p12 << endl;//output is the same as P12 references P10 << cout P10 Lt
Endl
    *P12 = 4000; Here is the error, cannot change the content that the constant pointer points to, but can point to other place p12 = P11;
    cout << p12 << Endl;
    cout << *p12 << Endl;
    cout << p11 << Endl;

cout << *p11 << Endl;  
    The T * Const &AMP;,CONST is outside the pointer, indicating that the reference can only point to a fixed place, but this fixed place value can be changed to int *const p14=&a; int *const p15 = &b;//declaration pointer p14 and P15 point to A and b int *const &p16 = p14;//reference, p16 is equivalent to P14, where p16 can only point to a as p14, and cannot point elsewhere.
    But you can change the value of a.
    cout << p16 << Endl;
    cout << p14 << Endl;
    cout << *p16 << Endl;
    cout << *p14 << Endl; *P16 = 1314;//*p14=1314 can cout << p16 <<
    Endl
    cout << p14 << Endl;
    cout << *p16 << Endl;
cout << *p14 << Endl;
    int *const &p16 = p15;//here wrong, p16 can no longer point to other places cout << p15 << Endl;
    cout << p16 << Endl;
    cout << *p15 << Endl;

cout << *p16 << Endl;
T *const & return 0; }

Conclusion: From the above experiment, we can find that the const is close to where the pointer is referenced and cannot be changed. such as int *const &p, it means that p is a constant pointer, and the place it points to cannot be changed, but it can change the number it points to. Or it is understood that the pointer variable p is constant and cannot be changed.
int const *&P,CONST is near the pointer asterisk, which means that the pointer points to a constant and cannot be changed. But pointer variable p can point to other places.

Pointers often refer to routines
int const *p1=&a;
int const *p2=&b;
int const *&p3=p1;
int const *&p3=p2;

For a constant reference to a variable, the experiment found that the int const &a=b,int Const &CONST A=B can change the value of a by changing the value of B, but this time it is not possible to change a alone. There is a bit of doubt here, but the experimental results do prove it.

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.