#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->mSize = arr.mSize;
this->pAddr = new T[this->mCapac
MyArray类模板实现
最新推荐文章于 2024-05-14 12:00:00 发布