返回   cpper编程论坛 > C/CPP/TMP/GP
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2007-11-10
初级会员
 
注册日期: 2006-10-18
帖子: 4
virus 正向着好的方向发展
默认 —[讨论]关于拷贝构造函数的疑问

#include<iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0) { X=xx;Y=yy;}
Point(Point &p);
int GetX() {return X;}
int GetY() {return Y;}
private:
int X,Y;
};
Point::Point(Point &p)
{
X=p.X;
Y=p.Y;
cout<<"point 拷贝构造"<<X<<Y<<endl;
}

class Line
{
public:
Line(Point xp1,Point xp2);
Line(Line &L);
double GtLen() {return len;}
private:
Point p1,p2;
double len;
};
Line::Line(Point xp1,Point xp2)1(xp1),p2(xp2)
{
cout<<"Line 构造"<<endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}

Line::Line(Line &L)1(L.p1),p2(L.p2)
{
cout<<"Line 拷贝构造"<<endl;
len=L.len;
}

int main()
{
Point myp1(1,1),myp2(4,5);
Line line(myp1,myp2);
Line line2(line);
cout<<"length of the line is:";
cout<<line.GtLen()<<endl;
cout<<"length of the line2 is:";
cout<<line2.GtLen()<<endl;
return 0;
}




代码如上,运行显示:
point 拷贝构造45
point 拷贝构造11
point 拷贝构造11
point 拷贝构造45
Line 构造
point 拷贝构造11
point 拷贝构造45
Line 拷贝构造
length of the line is:5
length of the line2 is:5
Press any key to continue


我的疑问是
1 为什么point的拷贝构造函数会先 执行myp2然后是myp1
2 为什么有四个拷贝构造函数呢?
我debug了一下,发现在执行
Line line(myp1,myp2);的时候 会先拷贝构造myp2(4,5),然后拷贝构造myp1(1,1),然后构造line,并拷贝构造myp1和myp2. 我主要是前两个点的拷贝构造不理解
谢谢大家的帮助啊
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2007-11-11
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 847
文章: 7
bankrock 正向着好的方向发展
默认 回复: —[讨论]关于拷贝构造函数的疑问

1.标准没有规定函数实参表达式的求值和传递顺序(大多数语言都有这个规定,所以不要对这方面做假设)
2.
代码:
Line line(myp1,myp2); Line line2(line);
这里每行都执行两个Point拷贝构造函数,加起来不就是四个了
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2007-11-11
初级会员
 
注册日期: 2006-10-18
帖子: 4
virus 正向着好的方向发展
默认 回复: —[讨论]关于拷贝构造函数的疑问

不对,你说错了
point一共执行了6次拷贝构造函数
line14次.line2的两次我明白,只是line1的4次不清楚
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2007-11-11
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 847
文章: 7
bankrock 正向着好的方向发展
默认 回复: —[讨论]关于拷贝构造函数的疑问

Sorry,是我搞错了

Line的构造函数:
代码:
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2)
这里参数传递拷贝了两次,初始化列表拷贝了两次,一共四次。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2007-11-11
初级会员
 
注册日期: 2006-10-18
帖子: 4
virus 正向着好的方向发展
默认 回复: —[讨论]关于拷贝构造函数的疑问

point 拷贝构造45 ////传的是值会调用copy ctor产生俩个临时变量,这个顺序应该是从右到左
point 拷贝构造11 ////所以会先是myp2构造
point 拷贝构造11 //初始化参数 用了两个
point 拷贝构造45
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 06:36 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0