C++ - 左值(lvalue)引用和右值(rvalue)引用 的 区别

左值(lvalue)引用和右值(rvalue)引用 的 区别

 

本文地址: http://blog.csdn.net/caroline_wendy/article/details/15029287 

 

左值引用, 即&i, 是一种对象类型的引用; 右值引用, 即&&i, 是一种对象值的引用;

std::move()可以把左值引用, 转换为右值引用;

左值引用是固定的引用, 右值引用是易变的引用, 只能引用字面值(literals)或临时对象(temporary object);

右值引用主要应用在移动构造器(move constructor)移动-赋值操作符(move-assignment operator)上面;

代码如下:

/*
 * cppprimer.cpp
 *
 *  Created on: 2013.11.7
 *      Author: Caroline
 */

#include <iostream>
#include <utility>

int main (void) {
	int i = 42;
	int &lr = i;
	int &&rr = i*42;
	const int &lr1 = i*42;
	int &&rr1 = 42;
	int &&rr2 = std::move(lr);
	std::cout << "i = " << i << std::endl;
	std::cout << "lr = " << lr << std::endl;
	std::cout << "rr = " << rr << std::endl;
	std::cout << "lr1 = " << lr1  <<std::endl;
	std::cout << "rr1  = " << rr1  << std::endl;
	std::cout << "rr2  = " << rr2  << std::endl;
}


 

C++ - 左值(lvalue)引用和右值(rvalue)引用 的 区别_第1张图片

你可能感兴趣的:(C++,Mystra,右值引用,移动构造器)