C++11的新特性:右值引用
2024-10-07 11:04:32
先看代码
#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); //类内声明
编译器仍旧会报错。
最新文章
- ASINetworkQueues(经典2)
- [php] php图表显示
- JS中的工厂模式
- Socket编程回顾,一个最简单服务器程序
- java基础复习之对于String对象,能够使用“=”赋值,也能够使用newkeyword赋值,两种方式有什么差别?
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- Discuz论坛下载与安装
- jquery 工作空间注册
- poj3662(二分+最短路)
- linux命令11
- Kotlin入门(30)多线程交互
- Huawei&#174; ENSP &; VRP CheatSheet
- Golang &; GitLab-CI 详细实例步骤
- python框架之Django(7)-Cookie&;Session使用
- hdu 4714 树+DFS
- 监控JVM内存使用情况,剩余空间小于2M时报警
- golang中的字符串拼接
- 服务商域名DNS大全
- tomcat服务器开启gzip功能的方法
- NodeJS待重头收拾旧山河(重拾)
热门文章
- React-Native 之 GD (五)属性声明和属性确认 及 占位图
- Deepin 系统安装并配置PHP开发环境
- 文件格式-CVS:CVS
- scrapy-splash常用设置
- 006-spring-data-elasticsearch 3.0.0.0使用【四】-spring-data之Elasticsearch Repositories
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_9_练习:按指定格式拼接字符
- 16/7/7_PHP-Static静态关键字
- Linux(Ubuntu)常用命令(三)
- ementUi rules表单验证 --》Wangqi
- 前端 CSS的选择器 基本选择器