文件1:"tpointer.h"
class TPoint
{
public:
TPoint(int x,int y){X=x;Y=y;}
TPoint(TPoint &p);
~TPoint(){cout<<"Destructer called!\n";}
int Xcoord(){return X;}
int Ycoord(){return Y;}
private:
int X,Y;
};
TPoint::TPoint(TPoint &p)
{ X=p.X;
Y=p.Y;
cout<<"copy_initialization Constructer called!\n";
}
文件2:
#include<iostream.h>
#include"tpointer.h"
TPoint f(TPoint Q);
void main()
{
TPoint M(20,35),P(0,0);
TPoint N(M);
P=f(N);
cout<<"P="<<P.Xcoord()<<","<<P.Ycoord()<<endl;
}
TPoint f(TPoint Q)
{
cout<<"ok\n";
int x,y;
x=Q.Xcoord()+10;
y=Q.Ycoord()+20;
TPoint R(x,y);
return R;
}
结果为:
copy_initialization Constructer called!
copy_initialization Constructer called!
ok
copy_initialization Constructer called!
Destructer called!
Destructer called!
Destructer called!
P=30,55
Destructer called!
Destructer called!
Destructer called!
Press any key to continue
把文件2的6行和8行改为:
6行:TPoint M(20,35);
8行:TPoint P=f(N);
之后结果为:
copy_initialization Constructer called!
copy_initialization Constructer called!
ok
copy_initialization Constructer called!
Destructer called!
Destructer called!
P=30,55
Destructer called!
Destructer called!
Destructer called!
Press any key to continue
请解释一下这个结果的函数调用顺序问题?十分感激!