#include
using namespace std;
#pragma once
template
class MyArray
{
public:
explicit MyArray(int capacity) //容量
{
this->m_Capacity=capacity;
this->m_Size=0;
//如果T是对象,那么这个对象必须提供默认的构造函数
pAddress=new T[this->m_Capacity]; //容量的大小
}
//拷贝构造
MyArray(const MyArray & arr)
{
this->m_Capacity=arr.m_Capacity;
this->m_Size=arr.m_Size;
this->pAddress=new T[this->m_Capacity];
for(int i=0;i
{
this->pAddress[i]=arr.pAddress[i];
}
}
//重载[]操作符arr[0]
T & operator [](int index)
{
return this->pAddress[index];
}
//尾插法
void Push_back(const T & val)
{
if(this->m_Capacity==this->m_Size)
{
return;
}
this->pAddress[this->m_Size]=val;
this->m_Size++;
}
void Pop_back()
{
if(this->m_Size==0)
{
return;
}
this->m_Size--;
}
int getSize()
{
return this->m_Size;
}
//析构函数
~MyArray()
{
if(this->pAddress!=NULL)
{
delete[]this->pAddress;
this->pAddress=NULL;
this->m_Capacity=0;
this->m_Size=0;
}
}
private:
T *pAddress; //指向一个堆空间,这个空间存储真正的数据
int m_Capacity; //容量
int m_Size; //大小
};
int main(int argc, char *argv[])
{
return 0;
}