引用的分类

  1. 普通左值引用:就是一个对象的别名,只能绑定左值,无法绑定常量对象
const int a = 10;
int& ref_a = a;  // wrong
  1. const左值引用:可以对常量起别名,可以绑定左值和右值
const int a = 10;
const int& ref1 = a;  // right
const int& ref2 = 10;  // right
  1. 右值引用:只能绑定右值的引用
int a = 10;
int&& rref1 = 20;     // right
int&& rref2 = a + 1;  // right
int&& rref3 = ++a;    // wrong,因为 ++a 为左值
int&& rref4 = ++a;    // right,因为 a++ 为右值
  1. 万能引用
posted @ 2023-05-08 16:29  hacker_dvd  阅读(33)  评论(0)    收藏  举报