有如下程序:
#include
usingnamespacestd;
classCon
charID;
public:
Con():ID(’A){cout<<1;}
Con(charID):ID(ID){eout<<2;}
Con(Con&c):ID(c.getID()){cout<<3;}
chargetID()corlst{returnID;}
};
voidshow(Conc)(cout< intmain()
{
Conc1:
show(c1);
Conc2(’B’);
show(c2);
return0;
}
执行上面程序的输出是
Eonc1定义c1对象,调用Con的无参数构造函数,输出1,ID值变为A,执行show(c1)时会调用复制构造函数,将c1对象的值复制给show函数的形参,此时输出3,然后在show()中输出c1的ID值A,Conc2(’B’)定义c2对象,调用Con(charID)构造函数,输出2,c2的ID值为B,show(c2)执行时先调用复制构造函数输出3,然后输出B。因此程序输出结果为“13A238”。