#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
template <class T>
class MyArray
{
public:
MyArray(int capacity)
{
this->mCapacity = capacity;
this->mSize = 0;
//申请内存
this->pAddr = new T[this->mCapacity];
}
MyArray(const MyArray<T> &arr)
{
this->mSize = arr.mSize;
this->mCapacity = arr.mCapacity;
this->pAddr = new T[this->mCapacity];
for (int i = 0; i < this->mSize; i++)
{
this->pAddr[i] = arr.pAddr[i];
}
}
~MyArray()
{
if (this->pAddr != NULL)
{
delete[] this->pAddr;
}
}
T &operator [] (int index)
{
return this->pAddr[index];
}
MyArray<T> operator = (const MyArray<T> &arr)
{
if (this->pAddr != NULL)
{
delete[] this->pAddr;
this->pAddr = NULL;
}
this->mCapacity = arr.mCapacity;
this->
MyArray类模板实现
最新推荐文章于 2022-07-24 19:26:37 发布
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <string>using namespace std;template <class T>class MyArray{public: MyArray(int capacity) { this->mCapacit...
摘要由CSDN通过智能技术生成