References to C + +

Source: Internet
Author: User

Directory

    • References to C + +
      • 1. What is a reference
      • 2. Why introduce references
      • 3. Referenced rules
      • 4. References and examples of variables
      • 4.3. References to array variables
      • 5. Frequently quoted and examples (keyword const)
C + + Reference 1, what is a reference

A reference is an alias for a thing (variable, data type). In fact, the variable name itself is a reference to a memory, that is, a name for that segment of memory, the name of the segment memory = the variable name defined.

2. Why introduce references
    • C + + introduces a reference, instead of a pointer-passing argument, the reference is completely equal to the value of the pass.
    • C inside the parameter is passed through the pointer, when the argument is an array, at this time with the pointer to pass formal parameters, the pointer has been degraded. (Coefficient a reference to the group variable).
3. Referenced rules

3.1 . A reference is a relational declaration, not a definition. Cannot exist independently, must be initialized, be consistent with the original type, and do not allocate memory.

  //错误写法                //正确写法  int a;                  int a;  int &mya;               int &mya = a; //引用本质是声明,不能先定义,后赋值。

3.2, the declaration of the relationship, once declared, cannot be changed.

  //错误写法                //正确写法  int a,b;                int a,b;  int &mya = a;           int &mya = a;  int &mya = b;           int &myb = a;

3.3, the reference can be referenced again, the result of multiple references is that a variable has multiple aliases, the number of individual names is a parallel relationship.

  int a;          int &mya = a;  int &myaa = a;    

3.4, the identification of references and other, & before the data type or & on the left of the assignment is the = reference, the other is the address or bitwise with.

4. References and examples of variables

4.1 . References to general variables

  int a;          int &mya = a;  //此时操作mya完全就是操作a

4.2 . Reference to pointer variable

  int *p;  int * &myp = p;

There is a problem: Does the referenced pointer exist?

  //不存在下列写法  int *p;  int * &myp = p;       //myp是一个指针的引用  int & * prp = &myp;  //&myp是指引用的地址  引用的本质是指针, C++对裸露的内存地址(指针)作了一次包装。又取得了指针的优良特性。所以再对引用取地址,建立引用的指针无意义。

4.3 . Multiple references to variables

  //即为一个变量起多个别名  int a;          int &mya = a;  int &myaa = a;
  • . There is a problem: Does the referenced reference exist?
4.3. References to array variables
#include <iostream>using namespace std;void fun(int (&data)[3]){    printf("sizeof(ra) = %d\n",sizeof(data));  //输出:12}void fun1(int *const &data){    printf("sizeof(pr) = %d\n",sizeof(data)); //输出:4}int main(){    int array[3];    fun(array);    fun1(array);    return 0;}

Summary: Two representations of the formal parameters of an array reference

    • int (&data)[3] //数组引用的标准形式值传递,sizeof(data) = 4*3=12。
    • int *const &data //其实已经退化为指针的引用,sizeof(data) = 4。

1. Const must be added here, because the array name is a const type;
2, when the array reference cannot manipulate the reference name of the array, such as: data = 2;
3, because the reference data is a const type, only the reference content can be manipulated: data[x] = 2.

There is a problem: Does the reference array exist?
Problem Resolution: There is no reference array, the reference is simply declared, memory is not allocated, and the array is allocated memory, which does not match the reference definition.
int &a[2] = {a,b} //错误写法。

5. Frequently quoted and examples (keyword const)

Concept: The original meaning of const, that is, cannot be modified. So a const variable or object can only be declared as a const reference, keeping its semantics consistent.

1. A non-const variable or object can be declared either as a const reference or as a no-const reference. Declared as a const reference, you cannot modify the data by using a const reference.

//non-const引用int data;int &rd = data;rd = 5;//const引用int data;const int &rd1 = data;//rd = 6; 声明为 const 引用,则不可以通过 const 引用修改数据。

2. A const variable or object must be declared as a const reference.

    const int val = 10;    int & rv = val; //错误写法    const int & rv2 = val; //正确写法

5.1. Common references to temporary objects

Temporary objects: an object that is usually not addressed, and the intermediate variables that the CPU produces in the run calculation, often referred to as rvalue. Common temporary objects are constants, expressions, and so on.

#include <iostream>using namespace std;//临时变量 即不可取地址的对象#include <iostream>using namespace std;//临时变量 即不可取地址的对象//常量 表达式 函数返回值 类型不同的变量int foo(){    int a = 100;    return a;}int main1(){    //常量    const int & cc = 55;    cout<<cc<<endl;    //表达式    int a = 3; int b = 5;    const int &ret = a+b;    //函数返回值    const int& ra = foo();    //类型不同的变量    double d = 100.12;    const int &rd = d;    }int main1(){    //常量    const int & cc = 55;    cout<<cc<<endl;    //表达式    int a = 3; int b = 5;    const int &ret = a+b;    //函数返回值    const int& ra = foo();    //类型不同的变量    double d = 100.12;    const int &rd = d;    return 0;}   
    • The nature of oft-quoted
#include <iostream>using namespace std;int main(){    double d = 3.14;    const int & rd = d;           cout<<"d = "<<d<<endl;        cout<<"rd = "<<rd<<endl;    d = 4.14;    cout<<"d = "<<d<<endl;    cout<<"rd = "<<rd<<endl;    return 0;}输出结果:    3.14     3    4.14     3

There is a problem: Why is the temporary variable changed, and the content of the reference variable remains the first value?
Problem Resolution: Inherently const reference, referencing an immutable temporary variable, const int TMP = DATA;CONST int &rd = tmp; At this point, we change the value of data, and the value of the temporary variable does not change.

References to C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.