C++中的左值、右值、左值引用、右值引用

简介: C++中的左值、右值、左值引用、右值引用

来看一个示例:

#define func(x) __func(x, "func(" #x ")")
void __func(int &x, const char *str) {
  cout << str << " is left value" << endl;
  return;
}
void __func(int &&x, const char *str) {
  cout << str << " is right value" << endl;
  return;
}
int main() {
  int x = 1234, y = 456;
  func(1234);
  func(x);
  func(x + y);
  func(x++);
  func(++x);
  func(x + 123);
  func(x *= 2);
  func(y += 3);
  func(y * 3);
  return 0;
}

程序输出:

func(1234) is right value
func(x) is left value
func(x + y) is right value
func(x++) is right value
func(++x) is left value
func(x + 123) is right value
func(x *= 2) is left value
func(y += 3) is left value
func(y * 3) is right value

一个简单的判断左值还是右值的方法:到了下一行还能不能通过单一变量访问到该值。能便是左值,否则是右值,其中字面量一定是右值。

比如func(x)x的值在下一行可以访问到,那么x就是左值

func(x + y)x + y的值在下一行访问不到,那么x + y就是右值

其他同理。

再来看一组示例:

#define func(x) __func(x, "func(" #x ")")
#define func2(x) __func2(x, "func2(" #x ")")
void __func2(int &x, const char *str) {
  cout << str << " is left value" << endl;
  return;
}
void __func2(int &&x, const char *str) {
  cout << str << " is right value" << endl;
  return;
}
void __func(int &x, const char *str) {
  cout << str << " is left value" << endl;
  func2(x);
  return;
}
void __func(int &&x, const char *str) {
  cout << str << " is right value" << endl;
  func2(x);
  return;
}

fun函数中调用func2函数,程序输出:

func(1234) is right value
func2(x) is left value
func(x) is left value
func2(x) is left value
func(x + y) is right value
func2(x) is left value
func(x++) is right value
func2(x) is left value
func(++x) is left value
func2(x) is left value
func(x + 123) is right value
func2(x) is left value
func(x *= 2) is left value
func2(x) is left value
func(y += 3) is left value
func2(x) is left value
func(y * 3) is right value
func2(x) is left value

可以发现,调用func2函数都是以左值的方式调用的,即使原来的值是右值。

比如func(y * 3)调用是右值对应的函数,而在func2中却调用了左值对应的函数。如果我们仍然想以右值的特性向下传递,可以利用move语义,让他以右值的属性向下传递

void __func(int &&x, const char *str) {
  cout << str << " is right value" << endl;
  //向下传递右值属性
  func2(move(x));
  return;
}

这时输出:

func(1234) is right value
func2(move(x)) is right value
func(x) is left value
func2(x) is left value
func(x + y) is right value
func2(move(x)) is right value
func(x++) is right value
func2(move(x)) is right value
func(++x) is left value
func2(x) is left value
func(x + 123) is right value
func2(move(x)) is right value
func(x *= 2) is left value
func2(x) is left value
func(y += 3) is left value
func2(x) is left value
func(y * 3) is right value
func2(move(x)) is right value

还可以使用forward向下传递右值属性

void __func(int &&x, const char *str) {
  cout << str << " is right value" << endl;
  func2(forward<int &&>(x));
  return;
}

move和forward可以理解是为了调用到正确的重载函数版本。

再来看一下右值引用在面向对象中的应用

class myvector {
public:
  myvector(int n = 10) : __size(n), data(new int[n]) {
    cout << this << endl;
    cout << "default constructor" << endl;
  }
  //左值引用
  myvector(const myvector &v) : __size(v.size()), data(new int[__size]) {
    cout << this << endl;
    cout << "deep copy constructor" << endl;
    for (int i = 0; i < size(); i++) data[i] = v[i];
    return;
  }
  myvector operator+(const myvector &v) {
    myvector ret(v.size() + this->size());
    myvector &now = *this;
    for (int i = 0; i < size(); i++) {
      ret[i] = now[i];
    }
    for (int i = size(); i < ret.size(); i++) {
      ret[i] = v[i - size()];
    }
    return ret;
  }
  int &operator[](int ind) const {
    return this->data[ind];
  }
  int size() const { return __size; }
  ~myvector() {
    cout << this << endl;
    cout << "destructor" << endl;
    if (data) {
      delete[] data;
    }
    data = nullptr;
  }
private:
  int __size;
  int *data;
};
ostream &operator<<(ostream &out, const myvector &v) {
  for (int i = 0; i < v.size(); i++) {
    out << v[i] << " ";
  }
  return out;
}
int main() {
  myvector v1, v2;
  for (int i = 0; i < v1.size(); i++) v1[i] = rand() % 100;
  for (int i = 0; i < v2.size(); i++) v2[i] = rand() % 100;
  myvector v3(v1 + v2);
  cout << v1 << endl;
  cout << v2 << endl;
  cout << v3 << endl;
  return 0;
}

实现一个vector,程序输出:

中间调用了深拷贝,若通过右值引用,实现一个移动拷贝:

//右值引用,移动构造
myvector(myvector &&v) : __size(v.size()), data(v.data) {
  cout << this << endl;
  cout << "move copy constructor" << endl;
  v.data = nullptr;//这里一定要记得
  v.__size = 0;
}

移动拷贝不用一个个拷贝数据,而是直接拿取临时对象的值

程序输出:

如果我们想直接抢夺一个对象的资源,而不是拷贝,就可以通过move来调用移动构造函数。比如:

int main() {
  myvector v1, v2;
  for (int i = 0; i < v1.size(); i++) v1[i] = rand() % 100;
  for (int i = 0; i < v2.size(); i++) v2[i] = rand() % 100;
  myvector v3(v1 + v2);
  cout << "v1: " << v1 << endl;
  cout << "v2: " << v2 << endl;
  cout << "v3: " << v3 << endl;
  myvector v4(move(v1));//v1中的数据给v4,move向下传递右值引用
  cout << "v1: " << v1 << endl;
  cout << "v4: " << v4 << endl;
  return 0;
}

关于引用的绑定顺序:

#include <iostream>
using namespace std;
void func1(int &x) {
        cout << __PRETTY_FUNCTION__ << "called" << endl;
        return;
}
void func1(const int &x) {
        cout << __PRETTY_FUNCTION__ << "called" << endl;
        return;
}
void func1(int &&x) {
        cout << __PRETTY_FUNCTION__ << "called" << endl;
        return;
}
void func1(const int &&x) {
        cout << __PRETTY_FUNCTION__ << "called" << endl;
        return;
}
int main() {
        int n;
        const int y = 123;
        func1(n);// 优先绑定func1(int &);
        func1(y);// func1(const int &);
        func1(123 + 456);// func1(int &&);
        return 0;
}

程序输出:

值得注意的是,const左值引用可以绑定任何值,比如:

#include <iostream>
using namespace std;
//只有const左值引用时
void func1(const int &x) {
        cout << __PRETTY_FUNCTION__ << "called" << endl;
        return;
}
int main() {
        int n;
        const int y = 123;
        func1(n);// 优先绑定func1(int &);
        func1(y);// func1(const int &);
        func1(123 + 456);// func1(int &&);
        return 0;
}

程序输出:

相关文章
|
2月前
|
存储 安全 C++
【C++11】右值引用
C++11引入的右值引用(rvalue references)是现代C++的重要特性,允许更高效地处理临时对象,避免不必要的拷贝,提升性能。右值引用与移动语义(move semantics)和完美转发(perfect forwarding)紧密相关,通过移动构造函数和移动赋值运算符,实现了资源的直接转移,提高了大对象和动态资源管理的效率。同时,完美转发技术通过模板参数完美地转发函数参数,保持参数的原始类型,进一步优化了代码性能。
47 2
|
4月前
|
编译器 C++
C++ 11新特性之右值引用
C++ 11新特性之右值引用
60 1
|
6月前
|
存储 安全 C++
浅析C++的指针与引用
虽然指针和引用在C++中都用于间接数据访问,但它们各自拥有独特的特性和应用场景。选择使用指针还是引用,主要取决于程序的具体需求,如是否需要动态内存管理,是否希望变量可以重新指向其他对象等。理解这二者的区别,将有助于开发高效、安全的C++程序。
46 3
|
6月前
|
存储 自然语言处理 编译器
【C++入门 三】学习C++缺省参数 | 函数重载 | 引用
【C++入门 三】学习C++缺省参数 | 函数重载 | 引用
|
6月前
|
C++
C++基础知识(二:引用和new delete)
引用是C++中的一种复合类型,它是某个已存在变量的别名,也就是说引用不是独立的实体,它只是为已存在的变量取了一个新名字。一旦引用被初始化为某个变量,就不能改变引用到另一个变量。引用的主要用途包括函数参数传递、操作符重载等,它可以避免复制大对象的开销,并且使得代码更加直观易读。
|
6月前
|
存储 自然语言处理 编译器
|
6月前
|
安全 C++
|
16天前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
58 19
|
16天前
|
存储 编译器 数据安全/隐私保护
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
40 13