Y
yyhhnn
Unregistered / Unconfirmed
GUEST, unregistred user!
#include <iostream.h>
class x
{
public:
x(int);
x(x&
~x();
int bb;
};
x::x(int aa)
{
cout<<"cons "<<aa<<endl;
bb=aa;
}
x::x(x&
cc)
{
bb=cc.bb+1;
cout<<"cons copy "<<bb<<endl;
}
x::~x()
{
cout<<" destruct "<<bb<<endl;
}
x f(x aa)
{
cout<<"in f"<<endl;
cout<<"aa "<<aa.bb<<endl;
return aa;
}
(甲)
void main()
{
x a(1);
x b(2);
a=f(b);
cout<<"a "<<a.bb<<endl;
}
为了显示清楚调用关系,我在拷贝构造函数中将bb的值加1 !!!,
运行结果:
cons 1
cons 2
cons copy 3
in f
aa 3
cons copy 4 //赋值操作不应该调拷贝构造的,为什么会有这行生成??
destruct 3
destruct 4 //请问这行是怎么生成的????????
a 4
destruct 2
destruct 4
(1)甲程序的cons copy 4 这一行的生成到底是临时对象的拷贝构造还是a的拷贝构造????
(2)甲程序的destruct 3是析构临时对象?还是f函数的局部对象aa析构??
(3)甲程序的destruct 4又是析构什么东东??
class x
{
public:
x(int);
x(x&
~x();
int bb;
};
x::x(int aa)
{
cout<<"cons "<<aa<<endl;
bb=aa;
}
x::x(x&
cc)
{
bb=cc.bb+1;
cout<<"cons copy "<<bb<<endl;
}
x::~x()
{
cout<<" destruct "<<bb<<endl;
}
x f(x aa)
{
cout<<"in f"<<endl;
cout<<"aa "<<aa.bb<<endl;
return aa;
}
(甲)
void main()
{
x a(1);
x b(2);
a=f(b);
cout<<"a "<<a.bb<<endl;
}
为了显示清楚调用关系,我在拷贝构造函数中将bb的值加1 !!!,
运行结果:
cons 1
cons 2
cons copy 3
in f
aa 3
cons copy 4 //赋值操作不应该调拷贝构造的,为什么会有这行生成??
destruct 3
destruct 4 //请问这行是怎么生成的????????
a 4
destruct 2
destruct 4
(1)甲程序的cons copy 4 这一行的生成到底是临时对象的拷贝构造还是a的拷贝构造????
(2)甲程序的destruct 3是析构临时对象?还是f函数的局部对象aa析构??
(3)甲程序的destruct 4又是析构什么东东??