#include <iostream>
using namespace std;
class Point{
private:
int x,y;
public:
Point(int a,int b=0){
x=a;y=b;
cout<<"normal"<<endl;
}
Point(const Point &p){
x=2*p.x;
y=2*p.y;
cout<<"copy"<<endl;
}
void print(){
cout<<x<<" "<<y<<endl;
}
~Point(){
cout<<"destructing"<<endl;
}
};
void f1(Point p){
p.print();
}
Point f3(Point p){
return p;
}
Point f2(){
Point p(10,30);
return p;
}
int main(){
Point p4(1,1);
cout<<"p4"<<endl;
p4.print();
Point p2=f3(p4);
p2.print();
return 0;
using namespace std;
class Point{
private:
int x,y;
public:
Point(int a,int b=0){
x=a;y=b;
cout<<"normal"<<endl;
}
Point(const Point &p){
x=2*p.x;
y=2*p.y;
cout<<"copy"<<endl;
}
void print(){
cout<<x<<" "<<y<<endl;
}
~Point(){
cout<<"destructing"<<endl;
}
};
void f1(Point p){
p.print();
}
Point f3(Point p){
return p;
}
Point f2(){
Point p(10,30);
return p;
}
int main(){
Point p4(1,1);
cout<<"p4"<<endl;
p4.print();
Point p2=f3(p4);
p2.print();
return 0;
}
结果:
normal
p4
1 1
copy
copy
destructing
4 4
destructing
destructing
若主函数为:
int main(){
Point p4(1,1),p2(1,1);
cout<<"p4"<<endl;
p4.print();
p2=f3(p4);
p2.print();
return 0;
}
结果:
normal
normal
p4
1 1
copy
copy
destructing
destructing//原来的p2析构
4 4
destructing
destructing