C++右值引用

2023-06-26,,

    右值引用是C++11标准引入的一个技术。

    与左值引用类似,右值引用的是右值,包括常量、临时值等不可作为左值的值,使用&&表示右值引用,如:type &&t = value1+value2;,在标准库的头文件<uility>有std::move()函数返回对应的右值类型。如果是const 左值引用类型,则同样可以接收右值。

    右值的应用不少,下面以一个简单的字符串存储类介绍其中的移动构造函数、移动赋值函数:

// a.h
#ifndef A_H
#define A_H
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;

class A
{
public:
    A(); // 默认构造函数
    A(const char* str); // 构造函数
    A(A &&a); // 移动构造函数
    A &&operator =(A &&a); // 移动赋值函数
    ~A(); // 析构函数
    void print(); // 输出mStr
private:
    int mLength;
    char *mStr;
};
#endif
// a.cpp
#include "a.h"

A::A()
{
    mLength = 0;
    mStr = nullptr;
}

A::A(const char *str)
{
    if (str != nullptr)
    {
        // 分配资源
        mLength = strlen(str);
        mStr = new char[mLength+1];
        strcpy(mStr,str);
    }
    else
    {
        A();
    }
}

A::A(A &&a)
{
    // 获取a的资源
    cout << "A(&&)" << endl;
    mLength = a.mLength;
    mStr = a.mStr;

    // 将a的mStr设为nullptr,防止a销毁时释放内存a.mStr
    a.mStr = nullptr;
    a.mLength = 0;
}

A &&A::operator =(A &&a)
{
    cout << "operator =(A&&)" << endl;
    if (mStr != nullptr)
    {
        delete []mStr;
        mStr = nullptr;
    }

    // 获取右值a的资源
    mStr = a.mStr;
    mLength = 0;
    // 防止右值a销毁时释放mStr的资源
    a.mStr = nullptr;
    a.mLength = 0;
    // 使用std::move()返回右值引用类型
    return std::move(*this);
}

A::~A()
{
    if (mStr != nullptr)
    {
        delete []mStr;
    }
}

void A::print()
{
    cout << mStr << endl;
}
// main.cpp
#include <iostream>
#include "A.h"
using std::cout;
using std::ends;

int main()
{
    A str1("asd");// 拷贝构造函数
    str1.print();

    str1 = "123"; // 移动赋值函数
    str1.print();

    A str2(A("zmh")); //移动构造函数
    str2.print();
    return 0;
}

    输出:

    asd
    operator =(A&&)
    123
    zmh

    使用右值引用时,要防止右值销毁而使获取的资源无效。

    以上是对右值引用的简单介绍,欢迎大家一起交流讨论。

《C++右值引用.doc》

下载本文的Word格式文档,以方便收藏与打印。