免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: yjqyml
打印 上一主题 下一主题

關於copy constructor 疑問~~ [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-11-08 13:24 |只看该作者
原帖由 yarco1 于 2005-11-8 13:12 发表
谢了. 楼上的. 学习了.

原来是LZ乱写...我说怎么看不懂.


lz写的程序是没有问题的,看不明白是因为程序不全

论坛徽章:
0
12 [报告]
发表于 2005-11-08 14:07 |只看该作者
原帖由 gooderfeng 于 2005-11-8 13:24 发表

lz写的程序是没有问题的,看不明白是因为程序不全

A::A(const A& a)
{
if (this !=&a)
{
delete count_;
count_ = new int [*a->count_];
}
else
count = 0;
return *this;

}

有没搞错a...a是A的一个shili的引用
居然用a->count_指针, 好歹也要a.count_

论坛徽章:
0
13 [报告]
发表于 2005-11-08 14:10 |只看该作者
原帖由 yarco1 于 2005-11-8 13:12 发表
谢了. 楼上的. 学习了.

原来是LZ乱写...我说怎么看不懂.



是我错了:

class BinStrTreeNode
BinStrTreeNode::BinStrTreeNode():
                                   _count(0),_leftchild(0),_rightchild(0)
{}

BinStrTreeNode::BinStrTreeNode(const BinStrTreeNode& rhs):_value(rhs._value)
{
_count = rhs._count;
if(rhs._leftchild)
   _leftchild = new BinStrTreeNode(*rhs._leftchild);
else
   _leftchild = 0;
if(rhs._rightchild)
   _rightchild= new BinStrTreeNode(*rhs._rightchild);
else
   _rightchild= 0;
}

论坛徽章:
0
14 [报告]
发表于 2005-11-08 14:15 |只看该作者
BinStrTreeNode &BinStrTreeNode:perator = (const BinStrTreeNode &rhs)
{
if(this != &rhs)
{
_value = rhs._value;
_count = rhs._count;
delete _leftchild;
if(rhs._leftchild)
   _leftchild = new BinStrTreeNode(*rhs._leftchild);
else
   _leftchild = 0;
delete _rightchild;
if(rhs._rightchild)
   _rightchild= new BinStrTreeNode(*rhs._rightchild);
else
   _rightchild= 0;
}
return *this;
}

再次道歉。。学业不精 。。哎 ~~~

论坛徽章:
0
15 [报告]
发表于 2005-11-08 14:43 |只看该作者
原帖由 renstone921 于 2005-11-8 12:44 发表


const A& a这定义了一个A引用类型的变量,它具有指针变量的语意,但有着普通变量的语法。

所以*a->count_这样的用法应当是错误的。

从你的问题可以得出,你没搞清楚构造函数和赋值运算符重载函数 ...

收藏此贴
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP