#include
INT & B = a; cout << b << endl; cout << & b << Endl; int & c = b; cout << c << endl; getchar (); * / / / test can be re-assigned / * int A = 100; int b = 200; int & c = a; cout << a << endl << b << endl << c << Endl; c = b; cout << c << endl; getchar (); * / / / An error usage of reference ........ / * int a = 100; int & b = & a; cout << a << endl << b << endl; getchar () ; * / / * Char * STR = "i am programming."; Char * & rpchar = str; cout << str << endl << rpchar << Endl; getchar (); * / // test reference and pointer mix The situation ... typef char * pchar; typedef char & rpo; typef pchar & rpchar; pchar str = "I am programming."; / * Rpchar rpchar; // WRONG: NOT INITIAALIZED ......... RPCHAR = Str; * / rpchar rpchar = str; cout << str << endl << rpchar << Endl; getchar ();} * // *********** / * Conclusion: For INT & A; reference is actually an implicit pointer, which is convenient to program, identifiable by compiler support. In the use of functions. It can be understood as a combination of follows. int (& a); in passing parameters During the process, you can follow the address value. The time is quite with a value during the function inside the function. You can see it as a convenient reason. In fact, reference & / * is a constant type that can automatically be reversely referenced by the compiler Pointer summary: ... / * a. General use: / * 1. int a = 100; / * int & b = a; / * 2. Int a = 100; / * int & b = a; / * int & c = B;
/ * 3. INT A = 100; / * int & b; // must define the assignment, different from the pointer (int A = 100; / * // int * p; p = & a;) / * b = a; / * 4. INT & A = 100; // Cannot give constants to 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 parameter type of the pass 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, the compile // is used as the address as the address. ///, if it returns a constant, then the constant is not // assignment / * INT & Fook INDEX) {RETURN (Arr [index] 1);} / * / * 2. Return local variable / * int & fook (param) {/ * int m = 100; / * return m; / *} / * // After the function ends the lifecycle, the returned address will not be available. / * / * 3. Not good to normal objects / * Class McLASS; / * mclass * mcptr; / * mcptr = new mclass (param); / * If (! Mcptr) MERROR ("Constructing Object Failed."); / * Mclass & mcref = * mcptr; / * Perhaps the reference to the above can make you feel more comfortable to use mclass: such as / * mcref.function (); Do not have to / * (* mcptr) .Function (); or mcptr-> function (); / * However, a serious problem is coming: memory leaks .. / * Because reference is not as obvious as pointers: You are likely to forget: delete & mcref; / * / * 4. Reference to objects related to objects / * Void Fook (param1) / * {/ * param-> function (noramlparam); / *} / * The above program, I want to pass an object's address, so that the object's member letter / * number is used .. What should I do? / * / * Void fook (mclass * mcpptrparam) {}; / * grace, can. / * Use one: / * mclass mcptr = new mclass (param); / * fook (mcptr); / * is also:
/ * McLASS MCOBJ; / * Fook (& MCOBJ); / * / * Of course you can also: / * void fook (mclass & mcrefparam) {}; / * This reference can be used in the global data area, stack, stack / * 5 Of course, is it convenient? ....... / * It is actually as it is in a function return value, can be identified as an address by the compiler, and there is also the same benefits in / * reference / * reference, the pointer is referenced Replace the pointer of the pointer, changeable / * work ... / * -------
81_redstar@163.com