一、左值、右值、左值引用&、右值引用&&
main.cpp
#include <iostream>
using namespace std;
int main()
{
string str = "I love china";
const char *p = str.c_str();
cout << (void*)p << endl;
string &str1 = str;//str为左值, str1为左值引用
string &&str2 = "I love china";//"I love china"为右值,str2为右值引用
string str3 = std::move(str);//str左值转右值,只有转右值才会调用移动构造函数,否则调用的是拷贝构造函数。
//调用了string的移动构造函数,原str为空,值转移到str3了
const char *p1 = str3.c_str();
cout << (void*)p1 << endl;//p和p1不一致,说明并不是名副其实的移动
string str4 = "I love china";
string && str5 = std::move(str4);//不会调用移动构造函数,只是个左值转右值
str5 = "abc";//str5和str4同步修改
str4 = "edf";//str5和str4同步修改
int i = 0;
int& i1 = i;//i是左值,i1是左值引用
int&& i2 = 1;//1是右值,i2是右值引用
int && i3 = s