[SOURCE DOWNLOAD]
Indispensable Windows Native-C + +: reference types
Webabcd
Introduced
Essential C + + of Windows Native
Example
CppReference.h
#pragma<string>usingnamespace std; namespace nativedll{ class cppreference { public: string Demo (); };}
CppReference.cpp
/** Reference type * * Reference can also be called "Alias" * Note: * 1, when declaring a reference, you must initialize * 2, the referenced object must have been allocated space * 3, the referenced object cannot be an address, that is, pointer variables, array variables, etc. cannot be referenced*/#include"pch.h"#include"CppReference.h" using namespaceNativedll;voidreference_demo1 ();voidReference_demo2 ();voidReference_demo3 ();stringcppreference::D emo () {//usage of referencesReference_demo1 (); //the difference between a reference and a pointerReference_demo2 (); //"Reference" can also be used as the return value of a functionReference_demo3 (); return "look at the code and the comments.";}//usage of referencesvoidReference_demo1 () {intA1, a2 =Ten; //&b-Represents the definition of a reference named B. The "&" here is a type specifier, indicating that B is a reference//declares a reference, it must be initialized at the same time int&b = A1;//B is a reference to A1, that is, B is the alias of A1b= A2;//A1 and B are equal to tenA1 = -;//A1 and B are equal tob = +;//A1 and B are equal to}//the difference between a reference and a pointervoidReference_demo2 () {intm =1; intn =2; int*x = &m; int*y = &N; int&s =m; int&t =N; voidMy_swap (int*i,int*J);//swap two integers with pointers voidMy_swap (int&i,int&J);//Exchange two integers by referenceMy_swap (x, y);//call void My_swap (int *i, int *j); Result: M=2,n=1My_swap (S, t);//call void My_swap (int &i, int &j); Result: m=1,n=2My_swap (M, n);//call void My_swap (int &i, int &j); Result: M=2,n=1}//swap two integers with pointersvoidMy_swap (int(Inint*j) { //The formal parameter is a copy of the argument, where the pointer is copied and released immediately after the function call ends inttemp; Temp= *i; *i = *J; *j =temp;}//Exchange two integers by referencevoidMy_swap (int&i,int&j) { //In the case of "references", I and J are actually the corresponding two arguments themselves . inttemp; Temp=i; I=J; J=temp;}//"Reference" can also be used as the return value of a functionint&reference_function ();intReference_i =0;voidReference_demo3 () {reference_function ()=999; //at this point the value of reference_i is 999}int&reference_function () {returnreference_i;}
Ok
[SOURCE DOWNLOAD]
Indispensable Windows Native-C + +: reference types