C++

Source: Internet
Author: User

The shape of the reference and the pointer is particularly similar, the obvious difference is that when the initialization, the pointer can be empty, then assign a value later, and the reference must be initialized at the time of Declaration, and after associating with a variable, will always be the variable "allegiance", it is more like a const type of pointer.

int &alia = val;

The above statement is actually an implicit expression of the following expression

Const int *ptr = val;

*ptr and &alia are of the same significance.

#include <iostream>using namespacestd;intMain () {intval =101; int&alia =Val; cout<<"val="<<val<<Endl; cout<<"alia="<<alia<<Endl; Alia++; cout<<"val="<<val<<Endl; cout<<"alia="<<alia<<Endl; cout<<"addr of val ="<<&val<<Endl; cout<<"addr of Alia ="<<&alia<<Endl; return 0;}

Both have the same value and address.

#include <iostream>using namespacestd;intMain () {intval =101; int&alia =Val; cout<<"val="<<val<<Endl; cout<<"alia="<<alia<<Endl; cout<<"addr of val ="<<&val<<Endl; cout<<"addr of Alia ="<<&alia<<Endl; intVal1 =202; Alia=Val1; cout<<"val1="<<val1<<Endl; cout<<"alia="<<alia<<Endl; cout<<"val="<<val<<Endl; cout<<"addr of val1 ="<<&val1<<Endl; cout<<"addr of Alia ="<<&alia<<Endl; return 0;}

It seems that the reference is changed by the assignment, in fact the variable represented by the reference is also changed, and the referenced address has not changed, so the reference is not changed (still loyal to the original variable).

int 101 ; int *ptr = &val; int &alia = *ptr; int 202  = &val1;

After the operation, the Alia reference remains unchanged.

Reference type Parameters

In the C language, there is only one way to pass a function argument without a value: pass a pointer. C + + provides multiple ways of communicating: passing a reference. The argument passed to the calling function is no longer a copy of the parameter variable, but the parameter variable itself (the same variable, with both formal and argument names).

#include <iostream>using namespacestd;voidSWAPV (intAintb);voidSwapp (int*p,int*q);voidSwapa (int&a,int&b);intMain () {intval1=1; intVal2=2; cout<<"Val1 ="<<Val1; cout<<"Val2 ="<<val2<<endl<<Endl; cout<<"Swap by alias!"<<Endl;    Swapa (Val1, val2); cout<<"Val1 ="<<Val1; cout<<"Val2 ="<<val2<<endl<<Endl; Val1=1; Val2=2; cout<<"Val1 ="<<Val1; cout<<"Val2 ="<<val2<<endl<<Endl; cout<<"Swap by pointers!"<<Endl; Swapp (&val1, &val2); cout<<"Val1 ="<<Val1; cout<<"Val2 ="<<val2<<endl<<Endl; Val1=1; Val2=2; cout<<"Val1 ="<<Val1; cout<<"Val2 ="<<val2<<endl<<Endl; cout<<"Swap by value!"<<Endl;    SWAPV (Val1, val2); cout<<"Val1 ="<<Val1; cout<<"Val2 ="<<val2<<endl<<Endl; return 0;}

Both the reference and the pointer change the value of the argument, so in the case of a call to use this by-address parameter, you can pass a const reference, so that it will be discovered by the compiler when the view modifies the parameter in the called function, without changing the value of the original variable. The reference is more restrictive than the value of the argument, and the reference is an alias of the variable, so the reference must not be processed in extra

void func (int &ra); int Ten ; int &alia =3.0);

The compiler will find an error because the reference changes the type of the parameter variable, when the compiler does not process the variable type, but instead throws the error directly (if it is a pass value, the compiler casts the variable type).

C++

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.