Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341426
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:07:19

我正在学习c++实用培训基础教程(人民邮电出版社出版,杨明军,董亚卓,江黎编著),我遇到了一个关于拷贝构造函数是“值拷贝”还是“位拷贝”的问题,我是用Borland c++3.1和turbo c++3.0编程,为了验证书中所说的拷贝构造函数是位拷贝的说法,我编写了下面的程序:
******************************************
#include
class family
{private:
  char *father;
  char *mother;
  char *son;
  char *daughter;
  public:
 void init(char *p1,char *p2, char *p3,char*p4);
  family();
  ~family();
  void display(void);
};
void family::init(char *p1="john",char *p2="harry",char *p3="jim",char *p4="kate")
{father=p1;
 mother=p2;
 son=p3;
 daughter=p4;
}
family::family()
{ init();}
family::~family()
{};
void family::display()
{ cout<<"the father is "<  cout<<"the mother is "<  cout<<"the son is "<  cout<<"the daughter is "<  cout<<"*************************"<}
void main()
{family a1;
 family a2(a1);
 a1.display();
 a2.display();
 a1.init("111","222","333","444");
 a1.display();
 a2.display();
}
******************************************************
程序输出结果如下:
the father is john
the mother is harry
the son is jim
the daughter is kate
*************************
the father is john
the mother is harry
the son is jim
the daughter is kate
*************************
the father is 111
the mother is 222
the son is 333
the daughter is 444
*************************
the father is john
the mother is harry
the son is jim
the daughter is kate
*************************
******************************************
我已经用a1.init("111","222","333","444");更改了husband,wife,son,daughter中的值,如果拷贝构造函数是“位拷贝”,则a2中的husband,wife,son,daughter也应该被改成111,222,333和444,但程序输出结果却还是原始默认值,此结果是否说明borlan c++3.1和turbo c++3.0中的拷贝构造函数是值拷贝而非位拷贝,是不是书中的说法是错误的?
请C++高手为我指点一下:-)

--------------------next---------------------

阅读(1009) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~