代码什么的要哭了 2022-05-03 15:16 采纳率: 67.9%
浏览 22
已结题

动态数组中构造函数为什么调用了两遍呢,求解答

不知道为什么动态数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

  • 写回答

1条回答 默认 最新

  • 昂格莱德 2022-05-09 23:57
    关注

    实参构造时会调用,new缺省也会调用默认构造函数,即以你的自定义构造函数的默认形参方式作为默认构造函数,不知你所说的两遍是什么意思

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月19日
  • 创建了问题 5月3日

悬赏问题

  • ¥15 关于#python#的问题:使用ATL02数据解算光子脚点的坐标(操作系统-windows)
  • ¥115 关于#python#的问题:未加密前两个软件都可以打开,加密后只有A软件可打开,B软件可以打开但读取不了数据
  • ¥15 在matlab中Application Compiler后的软件无法打开
  • ¥15 想问一下STM32创建工程模板时遇到得问题
  • ¥15 Fiddler抓包443
  • ¥20 Qt Quick Android 项目报错及显示问题
  • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
  • ¥15 oracleBIEE analytics
  • ¥15 H.264选择性加密例程
  • ¥50 windows的SFTP服务器如何能批量同步用户信息?