有三种情况会产生复制构造函数的调用!
在代码中只要都会<强> >强壮!
在代码中显示
# include
使用名称空间性病;
类位置{公共:
位置(int, int b) {x=a;y=b;}
位置(const Location&lc) {
cout & lt; & lt;“叫copy_constructor”& lt; & lt;endl;
x=lc.getx ();
y=lc.y;
}
~位置(){
cout & lt; & lt;“对象销毁”& lt; & lt;endl;
}
int getx () const {
返回x;
}
int gety () const {
返回y;
}
空白对于setx (int) {x=a;}
空白调用sety (int b) {y=b;私人:}
int x, y;
};
空白f(){这里会产生调用复制构造函数!因为需要产生临时对象强>
p.setx; (5),
p.sety (6);
cout & lt; & lt;“x=" & lt; & lt;p.getx () & lt; & lt;" y=" & lt; & lt;p.gety () & lt; & lt;endl;
}
<强> 强>{<强>//强> 强> <强>。强> <强>这里会产生调用复制构造函数强>
位置p (4、5);
返回p;
}
<强> Location& 强> h () {
cout & lt; & lt;“h”()& lt; & lt;endl; p
位置(4、6);
返回p;
}
空白func () {
位置location1 (2、3);
这里也产生调用复制构造函数强>
cout & lt; & lt;”位置。x=" & lt; & lt;location2.getx () & lt; & lt;endl;
cout & lt; & lt;”位置。y=" & lt; & lt;location2.gety () & lt; & lt;endl;
f (location2);
cout & lt; & lt;”位置。y=" & lt; & lt;location2.gety () & lt; & lt;endl;
location2=g ();
cout & lt; & lt;”位置。y=" & lt; & lt;location2.gety () & lt; & lt;endl;
location2=h ();
}
int主要(){
func ();
系统(“暂停”);
返回0;
}