📜  C++中带有示例的左值引用和右值引用

📅  最后修改于: 2021-05-30 03:51:29             🧑  作者: Mango

先决条件: C++中的左值和右值,C++中的引用

“ l值”是指识别对象的存储位置。 “ r值”是指存储在内存中某个地址处的数据值。 C++中的引用不过是已经存在的变量的替代方法。在变量名称前使用“&”声明它们。

例子:

int a = 10;

// Declaring lvalue reference
int& lref = a;

// Declaring rvalue reference
int&& rref = 20;

以下是左值和右值的实现:

C++
// C++ program to illustrate the
// lvalue and rvalue
  
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Declaring the variable
    int a{ 10 };
  
    // Declaring reference to
    // already created variable
    int& b = a;
  
    // Provision made to display
    // the boolean ouptut in the
    // form of True and False
    // instead of 1 and
    cout << boolalpha;
  
    // Comparing the address of both the
    // varible and its reference and it
    // will turn out to be same
    cout << (&a == &b) << endl;
    return 0;
}


C++
// C++ program to illustrate the
// lvalue and rvalue
#include 
using namespace std;
  
// Driver Code
int main()
{
    int a = 10;
  
    // Declaring lvalue reference
    // (i.e variable a)
    int& lref = a;
  
    // Declaring rvalue reference
    int&& rref = 20;
  
    // Print the values
    cout << "lref = " << lref << endl;
    cout << "rref = " << rref << endl;
  
    // Value of both a
    // and lref is changed
    lref = 30;
  
    // Value of rref is changed
    rref = 40;
    cout << "lref = " << lref << endl;
    cout << "rref = " << rref << endl;
  
    // This line will generate an error
    // as l-value cannot be assigned
    // to the r-vaue referances
    // int &&ref = a;
    return 0;
}


C++
// C++ program to illustrate lvalue
#include 
using namespace std;
  
// Creating the references of the
// parameter passed to the function
void swap(int& x, int& y)
{
    int temp = x;
    x = y;
    y = temp;
}
  
// Driver Code
int main()
{
    // Given values
    int a{ 10 }, b{ 20 };
    cout << "a = " << a
         << " b = " << b << endl;
  
    // Call by Reference
    swap(a, b);
  
    // Print the value
    cout << "a = " << a
         << " b = " << b << endl;
    return 0;
}


C++
// C++ program to illustrate rvalue
#include 
using namespace std;
  
// lvalue reference to the lvalue
// passed as the parameter
void printReferenceValue(int& x)
{
    cout << x << endl;
}
  
// Driver Code
int main()
{
    // Given value
    int a{ 10 };
  
    // Function call is made lvalue & can
    // be assigned to lvalue reference
    printReferenceValue(a);
    return 0;
}


C++
// C++ program to illustrate rvalue
#include 
using namespace std;
  
// Declaring rvalue reference to the
// rvalue passed as the parameter
void printReferenceValue(int&& x)
{
    cout << x << endl;
}
  
// Driver Code
int main()
{
    // Given value a
    int a{ 10 };
  
    // Works fine as the function is
    // called with rvalue
    printReferenceValue(100);
    return 0;
}


输出:
true

说明:由于两个变量都指向相同的内存位置,因此以下代码将输出True。 b只是分配给变量a的内存的替代名称。上面代码中声明的引用是左值引用(即,引用左值中的变量),类似地,也可以声明值的引用。

右值引用具有两个有用的属性

  1. 右值引用延长了为其分配了临时对象的寿命。
  2. 非常量右值引用允许您修改右值。

重要提示:左值引用可以用右值分配,但右值引用不能分配给左值

C++

// C++ program to illustrate the
// lvalue and rvalue
#include 
using namespace std;
  
// Driver Code
int main()
{
    int a = 10;
  
    // Declaring lvalue reference
    // (i.e variable a)
    int& lref = a;
  
    // Declaring rvalue reference
    int&& rref = 20;
  
    // Print the values
    cout << "lref = " << lref << endl;
    cout << "rref = " << rref << endl;
  
    // Value of both a
    // and lref is changed
    lref = 30;
  
    // Value of rref is changed
    rref = 40;
    cout << "lref = " << lref << endl;
    cout << "rref = " << rref << endl;
  
    // This line will generate an error
    // as l-value cannot be assigned
    // to the r-vaue referances
    // int &&ref = a;
    return 0;
}
输出:
lref = 10
rref = 20
lref = 30
rref = 40

左值引用的用途

  1. 左值引用可用于别名现有对象。
  2. 它们还可以用于实现按引用传递语义。

C++

// C++ program to illustrate lvalue
#include 
using namespace std;
  
// Creating the references of the
// parameter passed to the function
void swap(int& x, int& y)
{
    int temp = x;
    x = y;
    y = temp;
}
  
// Driver Code
int main()
{
    // Given values
    int a{ 10 }, b{ 20 };
    cout << "a = " << a
         << " b = " << b << endl;
  
    // Call by Reference
    swap(a, b);
  
    // Print the value
    cout << "a = " << a
         << " b = " << b << endl;
    return 0;
}
输出:
a = 10 b = 20
a = 20 b = 10

注意:当函数返回左值引用时,表达式变为左值表达式。

右值引用的用途

  1. 它们用于与move构造函数和move分配一起使用。
  2. 不能将类型’ int& ‘的非常量左值引用绑定到类型’ int ‘的右值。
  3. 不能将’ int && ‘类型的右值引用绑定到’int’类型的左值。

程序1:

C++

// C++ program to illustrate rvalue
#include 
using namespace std;
  
// lvalue reference to the lvalue
// passed as the parameter
void printReferenceValue(int& x)
{
    cout << x << endl;
}
  
// Driver Code
int main()
{
    // Given value
    int a{ 10 };
  
    // Function call is made lvalue & can
    // be assigned to lvalue reference
    printReferenceValue(a);
    return 0;
}
输出:
10

程式2:

C++

// C++ program to illustrate rvalue
#include 
using namespace std;
  
// Declaring rvalue reference to the
// rvalue passed as the parameter
void printReferenceValue(int&& x)
{
    cout << x << endl;
}
  
// Driver Code
int main()
{
    // Given value a
    int a{ 10 };
  
    // Works fine as the function is
    // called with rvalue
    printReferenceValue(100);
    return 0;
}
输出:
100
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”