#include <iostream>
using namespace std;
template<class T>
class MyArray {
friend void test01();
public:
MyArray(int capacity)
{
this->mCapacity = capacity;
this->mSize = 0;
this->ptr = new T[this->mCapacity];
}
MyArray(const MyArray<T>& another)
{
this->mCapacity = another.mCapacity;
this->mSize = another.mSize;
this->ptr = new T[this->mCapacity];
for (int i = 0; i < this->mSize; i++)
{
this->ptr[i] = another.ptr[i];
}
}
T& operator[](int index)
{
return this->ptr[index];
}
MyArray<T>& operator=(const MyArray<T>& another)
{
if (this->ptr != nullptr) {
delete[] this->ptr;
this->ptr = nullptr;
this->mCapacity = 0;
this->mSize = 0;
}
this->mCapacity = another.mCapacity;
this->mSize = another.mSize;
this->ptr = new T[this->mCapacity];
for (int i = 0; i < this->mSize; i++)
{
this->ptr[i] = another.ptr[i];
}
return *this;
}
void PushBack(T& data) {
if (this->mCapacity <= this->mSize) {
return ;
}
this->ptr[this->mSize] = data;
this->mSize++;
}
void PushBack(T&& data) {
if (this->mCapacity <= this->mSize) {
return ;
}
this->ptr[this->mSize] = data;
this->mSize++;
}
~MyArray() {
if (this->ptr != nullptr) {
delete[] this->ptr;
this->ptr = nullptr;
this->mCapacity = 0;
this->mSize = 0;
}
}
private:
int mCapacity;
int mSize;
T* ptr;
};
void test01() {
MyArray<int> marray(20);
int a = 10, b = 20, c = 30;
marray.PushBack(a);
marray.PushBack(b);
marray.PushBack(c);
marray.PushBack(1);
marray.PushBack(2);
marray.PushBack(3);
for (int i = 0; i < marray.mSize; i++) {
cout << marray[i] << " ";
}
}
int main() {
test01();
}
