C++函数的构造,析构问题
class B{public:
B(){ cout<<"default constructor"<<endl; }
~B(){ cout<<"destructed"<<endl; }
B(int i):data(i){ cout<<"constructed by parameter " << data <<endl; }
private:
int data;
};
B Play( B b){ return b ; }
int main(int argc, char* argv[]){
B temp = Play(5);
return 0;
}
为什么:
运行之后会有两次析构过程???? 但只有一次构造过程??? 因为是这样的
#include <iostream>
using namespace std;
class B{
public:
B(){ cout<<"default constructor"<<endl; }
B( B &b){ cout<<"copyConstructor"<<endl;}
~B(){ cout<<"destructed"<<endl; }
B(int i):data(i){ cout<<"constructed by parameter " << data <<endl; }
private:
int data;
};
B Play(B b)
{
return b ;
}
int main(int argc, char* argv[]){
B temp = Play(5);
return 0;
}
{:9_240:} DarkSE 发表于 2016-9-7 16:18
因为是这样的
shit!....
页:
[1]