//自己的通用的数组类
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int capacity)
{
cout << "MyArray的有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
MyArray(const MyArray& arr)
{
count << "MyArry的拷贝构造调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress=new T[arr.m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
MyArray&operator=(const MyArray & arr)
{
cout << "MyArry的operator=的调用" << endl;
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress=new T[arr.m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "析构函数调用" << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
T * pAddress;//指针指向堆区开辟的真实数组
int m_Capacity;//容量
int m_Size;//大小
};
拷贝构造函数中
this->m_Capacity = arr.m_Capacity;
这么写不行啊。成员变量都是私有的,arr.m_Capacity这么访问不可以。要加get函数