C++ 左值 右值

简单理解:左值 就是一般的变量,右值是临时的变量。

#include 
#include 

int main() {

    // 左值是有地址的,可以取地址
    // ptr 是 左值
    int ptr = 10;
    // 左值引用
    int &p = ptr;  // 引用 是变量的别名,通过指针实现的。


    // 右值 没有地址,临时变量,不能取地址
    // 右值引用
    const int &&b = 20;  // 20 没有地址,放入栈中,是临时地址
    // 表达式计算结果

    
    // move 作用
    // 作用二
    // move是将里面的数值,从 左值 转换 为 右值
    const int &&c = std::move(ptr);
    // 作用一 避免深拷贝
    std::string str1 = "qwer";
    // std::string str2 = str1;  // 深拷贝
    std::string str2 = std::move(str1);  // 移动资源 将 str1 里面存的内容放入 str2 中

    return 0;
}

你可能感兴趣的:(C++,基础,c++)