标题:怎样解释这个结果?
取消只看楼主
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
结帖率:100%
 问题点数:0 回复次数:0 
怎样解释这个结果?

文件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
请解释一下这个结果的函数调用顺序问题?十分感激!

搜索更多相关主题的帖子: 结果 解释 
2006-07-18 12:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-78973-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068056 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved