浅层复制:利用默认复制构造函数,在析构时会出错(因为析构了两遍)

深层复制:利用成员函数

题目:
1.
T& operator[](unsigned int i) ;
T& operator[](unsigned int i)const; //为什么用 const?
DynamicArray<int> iarray(length,-1);
DynamicArray<double> darray(length,-2.1);
const DynamicArray<char> carray(length,'c');
2.重载 = 运算符
先判断是否是同一个对象;
template <typename T>
DynamicArray<T>& DynamicArray<T>::operator= (const DynamicArray<T> & anotherDA) {
if(this == &anotherDA) {
cout<<endl<<"operator = is called";
return *this;
}
mallocSize = anotherDA.mallocSize;
array = new T[anotherDA.mallocSize];
for(int i = 0; i < anotherDA.mallocSize; i ++) array[i] = anotherDA.array[i];
cout<<endl<<"operator = is called";
return *this;
}
1021





