c++ 构造函数与析构函数
#include <iostream>using namespace std;
class Clock {
private:
int H,M,S;
public:
Clock(int h=0,int m=0,int s=0) {
H=h,M=m,S=s;
cout<<"constructor:"<<H<<":"<<M<<":"<<S<<endl;
}
~Clock() {
cout<<"destructor:"<<H<<":"<<M<<":"<<S<<endl;
}
Clock(Clock & p) {
H=p.H; M=p.M; S=p.S;
cout<<"copy const:"<<H<<":"<<M<<":"<<S<<endl;
}
};
Clock func(Clock C) {
Clock tTime(C);
return Clock(9,0,0); }
int main( ){
Clock C1(8,0,0);
func(C1);
return 0;
}
为什么得到constructor:8:0:0
copy const:8:0:0
copy const:8:0:0
consturctor:9:0:0
destructor:8:0:0
destructor:8:0:0
destructor:9:0:0
destructor:8:0:0
而不是8 8 8 9 9 8 8 8
不应该是后构造的先析构吗