c++析构函数和构造函数调用顺序的问题?
#include<iostream>using namespace std;
class TDate
{
int year,month,day;
public:
TDate(int y=2007,int m=1,int d=1);
TDate(const TDate &date);
~TDate();
void Print();
};
TDate::TDate(int y,int m,int d)
{
year = y;
month = m;
day = d;
cout<<"Constructor called.\n";
}
TDate::TDate(const TDate &date)
{
year = date.year;
month = date.month;
day = date.day;
cout<<"Copy Constructor called.\n";
}
void TDate::Print()
{
cout<<year<<"_"<<month<<"_"<<day<<endl;
}
TDate::~TDate()
{
cout<<"Destructor called.\n";
}
TDate f(TDate Q)
{
TDate R(Q);
return Q;
}
void main()
{
TDate day1(1998,5,26);
TDate day3;
TDate day2(day1);
TDate day4 = day2;
day3 = day2;
day3 = f(day2);
day3.Print();
}
输出如下:
Constructor called. // 1
Constructor called. // 2
Copy Constructor called. //3
Copy Constructor called. //4
Copy Constructor called. //5
Copy Constructor called. //6
Copy Constructor called. //7
Destructor called. //8
Destructor called. //9
Destructor called. //10
1998-5-26 //11
Destructor called. //12
Destructor called. //13
Destructor called. //14
Destructor called. //15
1-15都是由哪些函数调用的?能帮我都列出来吗?我初学者不太懂额