What is a reference variable in C++?

后端 未结 12 2110
孤城傲影
孤城傲影 2020-11-22 08:41

What would be a brief definition of a reference variable in C++?

相关标签:
12条回答
  • 2020-11-22 09:25

    A reference variable provides an alias (alternative name) for a previously defined variable. For example:

    float total=100;
    float &sum = total;
    

    It means both total and sum are the same variables.

    cout<< total;
    cout << sum;
    

    Both are going to give the same value, 100. Here the & operator is not the address operator; float & means a reference to float.

    0 讨论(0)
  • 2020-11-22 09:25

    Reference variables allow two variable names to address the same memory location:

    int main()
    {
        int var1;
    
        // var2 is a reference variable, holds same value as var1
    
        int &var2 = var1;
        var1 = 10;
    
        std::cout << "var1 = " << var1 << std::endl;
        std::cout << "var2 = " << var2 << std::endl;
    }
    

    Resource: LINK

    0 讨论(0)
  • 2020-11-22 09:26

    A Reference variable is an alias for the variable name.

    It is different from the pointers in following ways:

    1. You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
    2. Once a reference is initialized to an object, it cannot be changed to point to any another object whereas in case of pointer we can make it point to any other object at any time.
    3. A reference must be initialized the time it is created. Pointers can be made initialized at any time.
    0 讨论(0)
  • 2020-11-22 09:34

    It's a variable which references another one:

    int foo;
    int& bar = foo;
    

    bar is now a reference, which is to say that bar holds the location of memory where foo lies.

    See here for more information.

    0 讨论(0)
  • 2020-11-22 09:35

    Page 79 ~ 80 C++ Primer 5th ed.

    Object: A region of memory that has a type

    Variable: Named object or reference

    Reference: An alias for another object.

    0 讨论(0)
  • 2020-11-22 09:36

    A reference variable and a pointer variable are the exact same thing to the machine (the compiler will generate the same machine code).

    The most obvious advantages of using a reference variable over a pointer variable in my knowledge:

    1. Easy to understand (no address, de-reference all kinds of headache things)
    2. Saves you a tiny bit of typing, adn thus probably less error-prone.

    In the code below, the left side is using a reference variable, and the right side is using a pointer variable. They are the same thing to the machine, but you see the using reference variable saves you a little bit of typing.

    Reference variable           Pointer variable
    int a = 1;         ~~~~~~    int a = 1;
    int &b = a;        ~~~~~~    int *b = &a;
    b = 2;             ~~~~~~    *b = 2;
    cout << a << '\n'  ~~~~~~    cout << a << '\n'
    ==============================================
    2                  ~~~~~~    2
    
    0 讨论(0)
提交回复
热议问题