C++11的新特性:右值引用

先看代码

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

template <typename elemType>
class MyArray 
{
public:
    MyArray(int capacity);
    MyArray(const MyArray<elemType>& arr);
    ~MyArray();
    elemType& operator[](int index);
    MyArray<elemType> operator=(const MyArray<elemType>& arr);
    void PushBack(elemType& data);
    int GetSize() const { return this->mSize; }
    //void PushBack(T&& data);
private:
    int mCapacity;
    int mSize;
    elemType *pAddr;
};

template <typename elemType>
MyArray<elemType>::MyArray(int capacity)
{
    this->mCapacity = capacity;
    this->mSize = 0;
    this->pAddr = new elemType[this->mCapacity];
}

template <typename elemType>
elemType& MyArray<elemType>::operator[](int index)
{
    if (index > this->mCapacity || index < 0)
    {
        //异常
    }
    else
    {
        return *(this->pAddr + index);
    }
}

template<typename elemType>
void MyArray<elemType>::PushBack(elemType& data)
{
    if (this->mSize >= this->mCapacity)
    {
        //异常
        return;
    }
    else
    {
        *(this->pAddr + this->mSize) = data;
        this->mSize++;
    }
}

template<typename elemType>
MyArray<elemType>::MyArray(const MyArray<elemType>& arr)
{
    this->mCapacity = arr.mCapacity;
    this->mSize = arr.mSize;
    //申请内存空间
    this->pAddr = new elemType[this->mCapacity];
    //数据拷贝
    for (int ix = 0; ix < this->mSize; ++ix)
    {
        this->pAddr[ix] = arr.pAddr[ix];
    }
}

template<typename elemType>
MyArray<elemType>::~MyArray()
{
    if (this->pAddr != NULL)
    {
        delete[] this->pAddr;
    }
}

template<typename elemType>
MyArray<elemType> MyArray<elemType>::operator=(const MyArray<elemType>& arr)
{
    if (this->pAddr != NULL)
    {
        delete[] this->pAddr;
    }
    this->mCapacity = arr.mCapacity;
    this->mSize = arr.mSize;
    //申请内存空间
    this->pAddr = new elemType[this->mCapacity];
    //数据拷贝
    for (int ix = 0; ix < this->mSize; ++ix)
    {
        this->pAddr[ix] = arr.pAddr[ix];
    }
    return *this;
}



void test01()
{
    MyArray<int> marray(20);
    int a = 10;
    int b = 20;
    int c = 30;
    int d = 40;
    marray.PushBack(a);
    marray.PushBack(b);
    marray.PushBack(c);
    marray.PushBack(d);
    marray.PushBack(100);
    marray.PushBack(200);

    for (int ix = 0; ix < marray.GetSize(); ++ix)
    {
        cout << marray[ix] << " ";
    }
}

int main()
{
    test01();
    return 0;
}

代码模拟了STL中的array容器,编译代码,报错
1327401-20190619112848543-1290791738.png
报错的代码为

    marray.PushBack(100);
    marray.PushBack(200);

PushBack()的实现如下

template<typename elemType>
void MyArray<elemType>::PushBack(elemType& data)
{
    if (this->mSize >= this->mCapacity)
    {
        //异常
        return;
    }
    else
    {
        *(this->pAddr + this->mSize) = data;
        this->mSize++;
    }
}

其参数为引用,不能对右值取引用,也就是说

int i = &42;

这行代码是错误的。

    //不能对右值取引用
    //左值 可以在多行使用
    //右值 即临时变量,只能在当前行使用
    marray.PushBack(100);
    marray.PushBack(200);

解决办法:重载PushBack()函数

template<typename elemType>
void MyArray<elemType>::PushBack(elemType && data)
{
    if (this->mSize >= this->mCapacity)
    {
        //异常
        return;
    }
    else
    {
        *(this->pAddr + this->mSize) = data;
        this->mSize++;
    }
}

另:
在VS2017开发环境中,将PushBack()的函数实现如下

void PushBack(const elemType& data);    //类内声明

template<typename elemType>  //类外实现
void MyArray<elemType>::PushBack(const elemType& data)
{
    if (this->mSize >= this->mCapacity)
    {
        //异常
        return;
    }
    else
    {
        *(this->pAddr + this->mSize) = data;
        this->mSize++;
    }
}

这样在使用PushBack()时,编译不会报错

    marray.PushBack(100);
    marray.PushBack(200);

但在Linux下,gcc版本为4.4.6,即便是写为

void PushBack(const elemType& data);    //类内声明

编译器仍旧会报错。
1327401-20190619150800081-2059940782.png

转载于:https://www.cnblogs.com/Manual-Linux/p/11050387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值