C ++ basic work: starting from a left and right value, c C ++ reference

xiaoxiao2021-03-06  39

#include #include, stdio.h> / * -------------------- Compare program 1: Normal function-containing programs --- ------ àint a [3] = {1, 2, 3}; int function (int index); int main () {Int a; a = function (2); cout << a << endl Return 0;}

INT function (int index) {return a [index]; -----> Return value} * / ------------------------------------------------------------------------------------------------------ -------------------------------------------------- --->

/ * ------------------ Comparison program 2 -------------------------- ---------------- à

INT A [3] = {1, 2, 3}; int main () {int b = a [2]; --------------> right case, assignment value Cout << b << endl;

A [2] = 4; ---------------> Lord calculated COUT << a [2] << endl; return 0;} * /// ------> a [i] means the meaning of the left calculator and the right album -------------------------- -------------------------------------------------- ----- à

/ * ------------------------------------------ ------------------- à

INT i; int A [3] = {1, 2, 3}; int & function (int index); int main () {

Function (2) = 9; // ---------> can be assigned COUT << / * a [2] * / i << endl; return 0;}

INT & function (int index) {// return a [index]; returni} / * ----------------------------- ---------------------------------------- à

* /// ---------> Use the reference to implement the return type of the function .//--------- -> Why: Return a [index] Return Should be a shaped value // ------------> After using the reference, it has changed it over // ------------> How to achieve the above effects? .//----------------------> How to understand the role and meaning of the reference. Do you feel : & Nothing in several usage can be followed. Let's explore and summarize:

/ * Usecase * / by redstar81 # include #include

Using namespace std;

Void main (void) {// Test whether the reference can be configured with a constant. / * int & a = 100; // WRONG * / / / Trimouts / * int A = 100; int * PTR; PTR = & a; cout << * ptr << endl; getchar (); * // test the result returned to reference address / * int A = 100; int & b = a; cout << b << endl; cout << & b << endl; int & c = b; cout << c << endl; getChar (); * / / / test can be re-assised / * int A = 100; int b = 200; int & c = a; cout <

/ *********** / * Conclusion: For int & a; reference is actually an implicit pointer, it is convenient to program, identify by compiler support. In the use of functions. It is understood as a combination of the following. INT (& a); following the interior of the address value in the process of passing the parameters. / Quasi-value in the function within the function. You can see it as a convenient reason. In fact, reference & / * is a constant pointer summary that can automatically be reversed by the compiler: ... / * a. General use: / * 1. int a = 100; / * int & b = a; / * 2. INT a = 100; / * int & b; / * INT & C = B; / * 3. INT A = 100; / * int & b; // must define the value, different from the pointer (int A = 100; / * // int * p; p = & a;) / * b = a; / * 4. INT & A = 100; // cannot give a reference to a reference ...

/ * Const INT & A = 100; // Viable, make global view / * 5. int A = 100; / * int & b = a; / * cout << & b << Endl; // output a (also B) Address.. Instead of pointer / * // address ... / * 6. Int a = 100; / * int & b = a; / * int * Ptr; / * PTR = & a; / * cout << A << b << * ptr << Endl; // The result is the same .. Note * Ptr .. / * 7. Int a = 100; / * int b = 200; / * int & c = a; / * C = b; // Reference can be re-assigned ........... / * / * b. Bractical rules: / * 1. Series error usage: / * char * str = "i am Programming. "; / * Define reference arrays: INT & A [3]; (Define the argument array: int * a [3];) / * Define the referenced pointer: int & * a; Define pointer: int ** PTR ; / * Define references: int && a; / * 2. Define pointer reference: int * & a = str; // means / * be initialized when definedv. / * / * C. References in functions and object domains / * 1. Make functions return types Application: (of course, the type of parameter is referenced, then

/ * Is address delivery method ...) / * int Arr [3] = {1, 2, 3}; / * Fook (2) = 100; // The function returns to the reference, when doing the left value, compile

// The device uses it as an address ... / * //, if it returns constant, of course, it is not // assignment / * int & fook (int index) {return (arr [index] 1);} / * / * 2. Return local variable / * int & fook (param) {/ * int m = 100; / * Return m; / *} // // The returned address will not be available after the function ends the life cycle. / * / * 3. Well-to-ordinary objects / * Class McLAS; / * mclass * mcptr; / * mcptr = new mclass (param); / * if (! Mcptr) MERROR ("constructing object failed." ); / * Mclass & mcref = * mcptr; / * Perhaps the reference can make you feel more comfortable to use McLass: such as / * mcref.function (); without having / * (* mcptr) .Function (); or mcptr -> Function (); / * However, a serious problem is coming: memory disclosure.. / * Because reference is not as obvious as pointers: you are likely to forget: delete & mcref;


New Post(0)