C + + Reference

Source: Internet
Author: User

This article mainly records the reference features in C + + and summarizes the code examples and errors.

A simple reference

The reference is an attribute of C + +, and the pointer is the C language attribute. The above code results are as follows:
20
20
100
20
True
The referenced and referenced objects occupy the same memory unit and have the same address.

Pointer variable reference

For a reference, it is created with the description of which object is referenced, and will not modify the referenced object in the future. A reference is the equivalent of a variable alias.

Reference Error Example

Swap 2 strings
#include <iostream>#include<string.h>using namespacestd;voidSwapone (Char*&x,Char*&y)//reference to Pointers{    Char*temp; Temp=x; X=y; Y=temp;}voidSwaptwo (Char**x,Char**y)//Pointer to pointers{    Char*temp; Temp= *x; *x = *y; *y =temp;}voidSwapthree (Char*x,Char*y)//pointer Passing value (this method is not possible, equivalent to the value of the pass){//Some people misunderstand this because they may have used pointers to exchange 2 integers. This is still a difference.     Char*temp; Temp=x; X=y; Y=temp;}intMain () {Char*ap ="Hello";//*AP content is the content of H,*BP is H    Char*BP ="How is it ?"; cout<<"AP:"<< AP <<Endl; cout<<"BP:"<< BP <<Endl; //Swapone (AP,BP); //Swaptwo (&AP,&BP);Swapthree (AP,BP); cout<<"Swap ap,bp"<<Endl; cout<<"AP:"<< AP <<Endl; cout<<"BP:"<< BP <<Endl; return 0;}
Reference Error Example 2
#include <iostream>using namespacestd;Const floatPi=3.14f;//ConstantsfloatF//Global VariablesfloatF1 (floatR) {f= r*r*Pi; returnF;}float& F2 (floatR//return Reference{f= r*r*Pi; returnF; //the reference to the local variable returns (note the expiration date) and the system is functioning normally, but the result will be wrong.     /*float ff = f; return FF;*/}intMain () {floatF1 (float=5);//default parameter 5, you can modify the global variable f=78.5    float& F2 (float=5);//Ibid .    floatA=F1 (); //float& b=f1 ();//in the F1 () function, the value 78.1 of the global variable F is assigned to a temporary variable, temp (created implicitly by the compiler), and then the TEMP reference B is established, and an error occurs on a temp variable.    floatC=F2 (); float& D=f2 ();//instead of defining a variable, the main function uses a reference to the global variable directly. This way, in all 4 ways, the most memory-saving space.    But be aware of the validity period that it refers to. //the global variable F here must be valid longer than reference D, so it is safe. For example, a reference to a local variable is returned. D + =1.0f; cout<<"A ="<< a <<Endl; //cout<< "b =" << b <<endl;cout<<"C ="<< C <<Endl; cout<<"d ="<< D <<Endl; cout<<"f ="<< F <<Endl; return 0;}
Reference common Errors
#include <iostream>using namespacestd;classtest{ Public:    voidFConst int&Arg);Private:    intvalue;};intMain () {intA =7; Const intb =Ten; //int &c = b;//Error: B is constant, but C is not a constant reference, correct: const int &c=b;    Const int&d =A; A++; //d++;//Error, D is a constant reference, so you cannot modifytest test;    Test.f (a); cout<<"A ="<< a <<Endl; return 0;}voidTest::f (Const int&Arg) {    //arg = ten;//constants cannot be modifiedcout <<"arg ="<< Arg <<Endl; Value= -;}/** For variables of constant type, their references must also be of constant type. * For variables of a very variable type, it can be a very good amount. * However, note: You cannot use a constant reference to modify the value of a referenced variable in any situation. */

C + + Reference

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.