不知道为什么动态数Point类中,构造函数连续调用了两遍,求问
/*动动态数组——基本模板类,深copy构造,
深赋值的应用:Point类动态数组*/
#include<iostream>
using namespace std;
//实现一个Point类动态数组
class Point
{
private:
int x,y;
public:
//构造函数,输出 cout<<"\nPoint is called!"; 并完成私有成员的初始化
Point(int xx=0, int yy=0)
{
x = xx;
y = yy;
cout << "\nPoint is called!";
}
//析构函数,输出 cout<<"\n~Point is called!";
~Point()
{
cout<<"\n~Point is called!";
}
//友元输出函数,输出 "("<<p.x<<","<<p.y<<")";
friend ostream & operator << (ostream &out, Point &p)
{
out << "(" <<p.x << "," << p.y << ")";
return out;
}
};
template <typename T>
class DynamicArray
{
private:
T* array; //pointer ,一个T类型的指针
unsigned int mallocSize; //分配空间的大小。
public:
//Constructors
// cout<<endl<< "new T["<<this->mallocSize<<"] malloc "<< this->mallocSize << "*"<<sizeof(T)<<"="<<this->mallocSize *sizeof(T)<<" bytes memory in heap";
DynamicArray(unsigned length, const T &content) ; // mallocSize=length; 设置每个元素的初始内容是 content;
//Copy Constructor
// cout<<endl<< "Copy Constructor is called";
DynamicArray(const DynamicArray<T> & anotherDA ) ;
// Destructors
// cout<<endl<< "delete[] array free "<< this->mallocSize << "*"<<sizeof(T)<<"="<<this->mallocSize *sizeof(T)<<" bytes memory in heap";
~DynamicArray();
//return the this->mallocSize
unsigned int capacity() const;
// for the array[i]=someT.
T& operator[](unsigned int i) ;
//自己定义个operator[] const 重载
const T& operator[](unsigned int i) const;
//自己定义个 operator = 重载
// 函数内要输出 cout<<endl<<"operator = is called";
DynamicArray<T> & operator= (const DynamicArray<T> & anotherDA );
};
template <typename T>
DynamicArray<T>::DynamicArray(unsigned length, const T &content )
{
mallocSize = length;
array = new T[mallocSize];
for(int i =0; i < length; i ++)
array[i] = content;
cout << endl << "new T[" << this -> mallocSize << "] malloc " <<
this -> mallocSize << "*" << sizeof(T) << "=" <<
this -> mallocSize *sizeof(T) << " bytes memory in heap";
}
template <typename T>
DynamicArray<T>::DynamicArray(const DynamicArray<T> & anotherDA )
{
mallocSize = anotherDA.mallocSize; //拷贝空间大小
array = new T[anotherDA.mallocSize]; //新申请空间
for(int i = 0; i < anotherDA.mallocSize; i ++) //拷贝数组成员
array[i] = anotherDA.array[i];
cout<<endl<< "Copy Constructor is called";
}
template <typename T>
DynamicArray<T>::~DynamicArray()
{
cout << endl << "delete[] array free " <<
this -> mallocSize << "*" << sizeof(T) << "=" <<
this -> mallocSize *sizeof(T) << " bytes memory in heap";
delete[] array;
}
template <typename T>
unsigned int DynamicArray<T>::capacity() const
{
return this->mallocSize;
}
template <typename T>
T& DynamicArray<T>::operator[](unsigned int i)
{
return array[i];
}
template <typename T>
const T& DynamicArray<T>::operator[](unsigned int i) const
{
return array[i];
}
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;
}
//StudybarCommentBegin
int main()
{
int length,i;
cin>> length;
DynamicArray<Point> iarray(length,Point(3));
DynamicArray<Point> iarray2(iarray),iarray3(iarray2);
cout<<endl;
for(i=0;i<length;i++) {
cout << iarray3[i] <<" ";
iarray[i] = Point(i,i+1);
}
iarray3=iarray2=iarray;
cout<<endl;
for(i=0;i<length;i++) {
cout << iarray3[i] <<" ";
}
return 0;
}
//StudybarCommentEnd