C++右值引用

    右值引用是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

    使用右值引用時,要防止右值銷燬而使獲取的資源無效。

    以上是對右值引用的簡單介紹,歡迎大家一起交流討論。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章