MyArray數組筆記
#define _CRT_NO_SECURE_WARNINGS
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int capacity)
{
this->mcapacity = capacity;
this->msize = 0;
this->mpaddr = new T[this->mcapacity];
}
MyArray (const MyArray<T> &arr)
{
this->msize = arr.msize;
this->mcapacity = arr.mcapacity;
this->mpaddr = new T[this->mcapacity];
for ( i = 0; i < this->msize; i++)
{
this->mpaddr[i] = arr.mpaddr[i];
}
}
MyArray operator=(const MyArray<T> &arr)
{
if (this->mpaddr!=NULL)
{
delete[] this->mpaddr;
}
this->msize = arr.msize;
this->mcapacity = arr.mcapacity;
this->mpaddr = new T[this->mcapacity];
for (i = 0; i < this->msize; i++)
{
this->mpaddr[i] = arr.mpaddr[i];
}
return *this
}
T & operator[](int index)
{
this->mpaddr[index];
}
void PushBack(T &&data)
{
if (this->msize!=this->mcapacity)
{
this->mpaddr[msize] = data;
}
this->msize++;
}
~MyArray()
{
if (this->mpaddr!=NULL)
{
delete[] this->mpaddr;
}
}
public:
int msize;
int mcapacity;
int *mpaddr;
private:
};
void test()
{
MyArray<int>p(10);
p.PushBack(10);
p.PushBack(20);
for ( int i = 0; i < p.msize; i++)
{
cout << p.mpaddr[i] << endl;
}
}
int main(void)
{
test();
system("pause");
}