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

zhaozj2021-02-16  46

#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 }} int function (int index) {returnit A [index]; -----> Return value} * / --------------------- -------------------------------------------------- --------> / * ------------------ Comparison program 2 ----------------- -------------------------- àint a [3] = {1, 2, 3}; int main () {int b = a [ 2]; --------------> Right Operators, assigning Value cout << b << Endl; a [2] = 4; ---------- -----> Left Options, assigned COUT << a [2] << end1; return 0;} * /// --------> a [i] as a left calculator and right The mean of the operator is different ~/ --------------------------------------- -------------------------------------- à / * --------- -------------- Comparison program 3 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------- àINT I; INT A [3] = {1, 2, 3}; int}; int main () {function (2) = 9; // ---- -----> can be assigned COUT << / * a [2] * / i << endl; return 0;} int & function (int index) {// Return A [Index]; Return i;} / * ------------------------------------------------- -------------------- à * /// -----------> Using the reference to implement the function Return the type of change .//--------- -> Why: Return A [index] returned to the integer value // ------------> After the reference, it has changed over // ------------> Don't reference how to achieve the above effect .//-------- ---> How to make the returned right left Operator // ------------> How to understand the role and significance of the reference. Do you feel: & in a few usage, there is no consistent nature, and we will discuss and summarize it. : / * Usecase * / by redstar81 # include #include using namespace std; void main (void) {// test reference can be configured with constant. / * Int & a = 100; // Wrong * / / / / = 100; int * Ptr; PTR = & a; cout << * ptr = & a; cout << * Ptr = & a; cout << * Ptr << Endl; getchar (); * / / / Test returned Result / * int A = 100;

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

转载请注明原文地址:https://www.9cbs.com/read-23598.html

New Post(0)