zoukankan      html  css  js  c++  java
  • 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容器,编译代码,报错

    报错的代码为

    	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);    //类内声明
    

    编译器仍旧会报错。

  • 相关阅读:
    liunx下一些服务小知识
    hausaufgabe--python 32
    hausaufgabe--python 31
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xbc in position 21: illegal multibyte sequence
    hausaufgabe--python 30
    Running error: 'utf-8' codec can't decode byte 0xb4 in position 0: invalid start byte
    hausaufgabe--python 29- File 2
    Hausaufgabe--Python 28-- File 1
    hausaufgabe--python 27
    hausaufgabe--python 26 -Dict 2
  • 原文地址:https://www.cnblogs.com/Manual-Linux/p/11050387.html
Copyright © 2011-2022 走看看