博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11的新特性:右值引用
阅读量:6990 次
发布时间:2019-06-27

本文共 3196 字,大约阅读时间需要 10 分钟。

先看代码

#include "pch.h"#include 
#include
using namespace std;template
class MyArray {public: MyArray(int capacity); MyArray(const MyArray
& arr); ~MyArray(); elemType& operator[](int index); MyArray
operator=(const MyArray
& arr); void PushBack(elemType& data); int GetSize() const { return this->mSize; } //void PushBack(T&& data);private: int mCapacity; int mSize; elemType *pAddr;};template
MyArray
::MyArray(int capacity){ this->mCapacity = capacity; this->mSize = 0; this->pAddr = new elemType[this->mCapacity];}template
elemType& MyArray
::operator[](int index){ if (index > this->mCapacity || index < 0) { //异常 } else { return *(this->pAddr + index); }}template
void MyArray
::PushBack(elemType& data){ if (this->mSize >= this->mCapacity) { //异常 return; } else { *(this->pAddr + this->mSize) = data; this->mSize++; }}template
MyArray
::MyArray(const MyArray
& 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
MyArray
::~MyArray(){ if (this->pAddr != NULL) { delete[] this->pAddr; }}template
MyArray
MyArray
::operator=(const MyArray
& 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
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
void MyArray
::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
void MyArray
::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
//类外实现void MyArray
::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

你可能感兴趣的文章
Android如何获取多媒体文件信息
查看>>
端口列表详解
查看>>
ecshop 用户中心
查看>>
浅谈MySql的存储引擎(表类型)
查看>>
第三方控件DevExpress中ASPxNavBar1用法
查看>>
Spring.net、NHibernate相关文章导航
查看>>
Android中Application设置全局变量以及传值
查看>>
每日英语:The Rise Of The Female Investor
查看>>
黑马程序员-JAVA基础-基本数据类型对象包装类
查看>>
Gzip Zlib PNG 压缩算法,源码详解 - swo2006 - C++博客
查看>>
Console-算法[foreach,if]-一输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数...
查看>>
[原][问题解决]Openstck云平台虚拟机无法连接问题解决
查看>>
状态控件ios 中滑块、开关、分段控件、操作表和警告的常用函数
查看>>
分享非常漂亮的WPF界面框架源码及其实现原理
查看>>
如何获取ResultSet的行数和列数(转)
查看>>
绑定列ORA-24816: 在实际的 LONG 或 LOB 列之后提供了扩展的非 LONG 绑定数据
查看>>
Mobile Web调试工具Weinre
查看>>
Android巴士转发
查看>>
未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。
查看>>
linux编程掌握常用命令
查看>>