Easy Tutorial
❮ Android Tutorial Expandablelistview Es6 Object ❯

C++ Reference Types

Category Programming Techniques

Speaking of references, let's start with a story: In the courtyard where we live, there is a child named Luo XX. We like to give him a nickname. Seeing that he is not very well-nourished, with a big head and a small body, we gave him the nickname "Radish Head." So, Luo XX is his real name, and Radish Head is one of his aliases. So, what is a reference?

Definition: A reference is an alias for a variable.

Note: As an alias, a variable cannot only have an alias and not have its own real name.

References to Basic Data Types

Here is a piece of code as an example:

Example

#include <iostream>
using namespace std;

{
    int a = 3;
    int &b = a;  // b is a reference to a, meaning b is an alias for a.
                 // References must be initialized, otherwise the compiler will report an error.
    b = 10;
    cout << a << endl; // At this point, the value of a has changed from 3 to 10. Because no matter what operations we perform on the alias, they are actually operations on the variable itself. Just like when we ask Radish Head to do something, Luo XX himself goes to do it.
    return 0;
}

References to Structure Types

Here is a piece of code as an example:

Example

#include <iostream>
using namespace std;
// Define a structure named Coor, which is a coordinate.
typedef struct
{
    int x;
    int y;
} Coor;

int main(void)
{
    Coor c1;    // Define a structure variable called c1.
    Coor &c = c1;    // Give c1 an alias called c.
    c.x = 10;    // Assign a value to the data member of the structure variable through the reference.
    c.y = 20;
    cout << c1.x << c1.y;    // The output result: 10 20
    return 0;
}

References to Pointer Types

References to pointer types are the most complex and difficult to understand part of references, and they also have a unique form in writing.

Definition:

Type *&PointerReferenceName = Pointer;

Let's see an example:

Example

#include <iostream>
using namespace std;

int main(void)
{
    int a = 10;    // Define an integer variable a, the value of a is 10.
    int *p = &a; // Define a pointer pointing to the variable a
    int *&q = p;    // Define a reference to a pointer, that is, q is an alias for p. Definition method: Type *&PointerReferenceName = Pointer;
    *q = 20;    // Assign 20 to *q, which is equivalent to assigning 20 to *p, which is equivalent to assigning 20 to a.
    cout << a << endl;    // The output value of a is 20.
    return 0;
}

References as Function Parameters

Before learning about references, when we studied C language, we learned such a classic program:

// Swap two values
void fun(int *a, int *b) // The parameters are two integer pointers
{
    int temp = 0;    // Define a temporary variable. A good habit is to define a variable and initialize it;
    temp = *a;    // Assign *a to temp;
    *a = *b;    // Assign *b to *a;
    *b = temp;    // Then assign temp to *b; This completes the swap of the values of a and b.
}

int x = 10, y = 20;
fun(&x, &y); // When calling in the main function, the actual parameters passed need to be written as the address of a, the address of b, which is more troublesome and not easy to understand.

Now that we have learned about references, what should it look like to achieve the same functionality?

Example

void fun(int &a, int &b)    // The parameters are two integer references
{
    int temp = 0;
    temp = a;
    a = b;
    b = temp;
}

int x = 10, y = 20;
fun(x, y);    // When calling in the main function, after passing the actual parameters, a is an alias for x, and b is an alias for y.

>

Original article link: https://blog.csdn.net/stop_and_go/article

❮ Android Tutorial Expandablelistview Es6 Object ❯