??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲一区亚洲二区,久久人人爽人人爽爽久久,国产欧美午夜http://www.shnenglu.com/firehutter/雕栏玉砌应犹在,只是朱颜?/description>zh-cnSun, 24 Aug 2025 14:54:41 GMTSun, 24 Aug 2025 14:54:41 GMT60随笔http://www.shnenglu.com/firehutter/archive/2008/07/21/56792.html׃青菜的包?/dc:creator>׃青菜的包?/author>Mon, 21 Jul 2008 12:51:00 GMThttp://www.shnenglu.com/firehutter/archive/2008/07/21/56792.htmlhttp://www.shnenglu.com/firehutter/comments/56792.htmlhttp://www.shnenglu.com/firehutter/archive/2008/07/21/56792.html#Feedback0http://www.shnenglu.com/firehutter/comments/commentRss/56792.htmlhttp://www.shnenglu.com/firehutter/services/trackbacks/56792.html

]]>
C++学习(fn)W记Q类l承中关于虚函数和动态联~的几点问题http://www.shnenglu.com/firehutter/archive/2006/11/29/15785.html׃青菜的包?/dc:creator>׃青菜的包?/author>Wed, 29 Nov 2006 09:27:00 GMThttp://www.shnenglu.com/firehutter/archive/2006/11/29/15785.htmlhttp://www.shnenglu.com/firehutter/comments/15785.htmlhttp://www.shnenglu.com/firehutter/archive/2006/11/29/15785.html#Feedback0http://www.shnenglu.com/firehutter/comments/commentRss/15785.htmlhttp://www.shnenglu.com/firehutter/services/trackbacks/15785.html看v来,在大多数情况下,动态联~都更好Q因为它让程序能够选择为特定类型设计的Ҏ(gu)Q这样问题就来了(jin)Q既然动态联~这么好Qؓ(f)什么还要设计两U类型的联编Qؓ(f)什么默认的联编Ҏ(gu)是静(rn)态的而不是动态?

原因关键在于效率。Strousstrup说过Q很l典Q呵呵)(j)QC++的指导原则之一是,不要Z使用的特性付ZP内存或处理时_(d)(j)?br />
因ؓ(f)通常情况下,~译器处理虚函数的方法ؓ(f)Q给每个对象d一个隐藏成员,该成员(sh)保存?sh)(jin)一个指向函数地址数组的指针(UCؓ(f)虚函数表 virtual function table,vtblQ。虚函数表中存储?jin)?f)cd象进行声明的虚函数的地址。例如,基类包含一个指针,指向?jin)基cM所有虚函数的地址表,zcd象将包含一个指向独立地址表的指针Q如果派生类提供?jin)虚函数的新定义Q该虚函数表保存新函数的地址Q如果没有重新定义,则保留原始版本的地址。调用虚函数ӞE序查看存储在对象中的vtbl地址Q然后{向相应的函数地址表?br />
所以显而易见的是,使用虚函数时Q在内存和执行速度斚w有一定的额外成本Q包括:(x)
每个对象都将增大Q增大量为存储地址的空_(d)Ҏ(gu)个类Q编译器都创Z个虚函数地址表(数组Q;每个函数调用都需要执行一步额外的操作Q即到表中查扑֜址?br />
所以咱们要L的习(fn)惯是Q在设计cLQ可能包含一些不在派生类重新定义的成员函敎ͼ那么q些函数׃要设|ؓ(f)虚函数。这样首先会(x)有更好的效率Q其ơ被声明函数的成员函数就表明是预期在zcM?x)被重新定义的,在阅M码时也将比较方便?img src ="http://www.shnenglu.com/firehutter/aggbug/15785.html" width = "1" height = "1" />

]]>
得注意下伙食?jin)。?/title><link>http://www.shnenglu.com/firehutter/archive/2006/11/10/14969.html</link><dc:creator>׃青菜的包?/dc:creator><author>׃青菜的包?/author><pubDate>Fri, 10 Nov 2006 08:09:00 GMT</pubDate><guid>http://www.shnenglu.com/firehutter/archive/2006/11/10/14969.html</guid><wfw:comment>http://www.shnenglu.com/firehutter/comments/14969.html</wfw:comment><comments>http://www.shnenglu.com/firehutter/archive/2006/11/10/14969.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/firehutter/comments/commentRss/14969.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/firehutter/services/trackbacks/14969.html</trackback:ping><description><![CDATA[最q全面检查了(jin)下n体,拿到体检报告的时候,发现有一血液尿酸偏高,ȝ在后面批注,食动物内脏?br /><br />疑惑?jin)一下,正准备网上查查,l果有同事发?jin)封公司邮gQ正好是说这个事的,貌似出现该情늚Z有不。看?jin)看内容Q如下:(x)<br /><br /><font size="2">出现K偏高的现象,q是酸性体质的表现。是׃q食q多高蛋白、高脂肪、高p类食造成的。要改变偏酸性体质,关键在于饮食Q要多吃性食物。所谓食物的酸碱性,是说食物中的无机盐属于酸性还是属于碱性。一般金属元素钠、钙、镁{,在h体内其氧化物呈碱性,含这U元素较多的食物是<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q如大豆、豆腐、菠菜、莴W、萝卜、土豆、藕、洋葱、v带、西瓜、香蕉、梨、苹果、牛奶等。一些食物中含有较多的非金属元素Q如硫、氯{,在h体内氧化后,生成带有阴离子的酸根Q属于酸性食物。如猪肉、牛肉、鸡肉、鸭、蛋cR鲤鱹{牡蛎、虾Q以?qing)面_、大c뀁花生、大麦、啤酒等。所以不要误以ؓ(f)Ҏ(gu){吃h很酸是酸性食物,那是大错牚w的,刚好相反Q苹果是一U碱性的食物?br /><br /></font><font color="#000080" size="2">酸性体质,健康新杀?br /><br /> (tng) (tng) (tng)吃得好了(jin),w体却变 (tng)"??br /> (tng) (tng) (tng)"Z那年?~饭又少?如今q年?吃得真不?的确如此,C人生zLq不断在提高,每天大鱼大肉,K都离不开?q轻人去麦当功뀁肯德基吃洋快餐更是家常侉K.可是Ҏ(gu)一调查研I表?生活水^来好,ȝ水^来高,C文明病却来多;发病率越来越?(zhn)者越来越q轻化。有数字昄:我国目前的高血压h已辑ֈ1.6 (tng)个亿Q我国的高血脂h是 (tng)6500万,p尿病h (tng)4800万ƈ以每q?0%的速度高速增长,每年M癌症的有130 (tng)多万人,M?j)脑血疾病的?00 (tng)多万人?0q代我们国家呼吁要让高血压低_(d)可现在我们在高血压面前低?jin)头。我国心(j)脏病的发病年龄比80 (tng)q代初期整整提早?5q?也就是说原来50岁开始得?j)脏病?现在35岁的人就开始得?j)脏病。多么触目惊?j)的数字Q而这些数字只是官方统?q没有加上未(g)查出来的。我们不要?q到底是怎么?来多的医学家、营d家和研究机构为此现象讄Nq行研究Q最后得Z致结?酸性体质是病魔元凶?br /> (tng) (tng) (tng) (tng)挖出酸性体质的?br /> (tng) (tng) (tng) (tng)那么酸性体质是怎么来的?Cd研究向我们揭CZ(jin)真相:D蛋油米面等q些高蛋白、高脂肪、高p类食在ؓ(f)我们提供能量和营ȝ同时Q也在提供大量的酸性物质。那么到底什么物质生什么酸? (tng)我们一h看看: (tng)蛋白?主要来源于瘦肉、鱼、蛋{?在体内代谢生含}基酸、含h}基酸、尿酸等{?脂肪(主要来源于肥肉、a(b){?在体内代谢生烃丁酸、乙C?也称酮酸);x(chng)化合?p类Q主要来源于c面)在体内代谢生甘沚w、丙酮酸、^酸?br /> (tng) (tng) (tng) (tng)"?不除Q后(zhn)无I?br /> (tng) (tng) (tng) (tng)Z摄入的酸性食物过多,过?jin)h体酸^衡的调节能力Q肝肄w体重要部位负药作,Z的酸^衡就?x)被破坏Q体内酸碱pQ甚臛_现轻度酸中毒反应Q这是我们所说的酸性体质?br /> (tng) (tng) (tng) (tng)其实我们Zp个养鱼的池塘Q占Z体重70%的体液就是池塘里的水Q水被污染了(jin)鱼就要生病死亡,同样的道理,Z体液被酸性物质污染了(jin)Q组成我们h体的60万亿个细?yu)的生态环境就?x)发生变化,l胞(yu)׃(x)发生H变和病变而媄(jing)响我们的健康。我们都知道:酸性物质沉U在肝脏中会(x)产生脂肪?酸性物质沉U在肾脏中会(x)影响肄排泄功能QɞK偏高而生痛?酸性物质沉U在兌或组l器宫内引v相应炎症Q导致关节炎{疾?酸性物质沉U在胰腺中,?x)?jing)响胰岛细?yu)的zL、媄(jing)响胰岛素的敏感性引发糖病?qing)其q发症等{。更Z重的是酸性物质沉U在血液里、血壁使血壁增厚Q引起动脉硬化,引发中风、心(j)肌梗塞等各种?j)脑血疾病?br /><br />………?br /></font><font size="3"><br /><font size="2">含钠、钾、钙、镁{金属元素较多的乃是性食品。ƈ非味道酸的就是酸味食品,应该掌握一点:(x)所有酸味的水果、豆制品都ؓ(f)<font style="BACKGROUND-COLOR: #ffff00">性食?/font>而不是酸性食物,<font style="BACKGROUND-COLOR: #ffff00">性食?/font>被认为是h容作用的食物?br />豆腐、牛奶、芹菜、土豆、竹W、香菇、胡萝卜、v带、绿豆、香蕉、西瓜、草莓等?qing)大部分的蔬菜、水果都是碱性的?br />食物的“酸性”会(x)影响体态和皮肤健美?br /> l测定,q性的食物有:(x)豆腐、豌豆、大豆、绿豆、a(b)菜、芹菜、番薯、莲藕、洋葱、茄子、南瓜、黄瓜、蘑菇、萝卜、牛奶等。而呈性的食物有:(x)菠菜、白菜、卷?j)菜、生菜、胡萝卜、竹W、马铃薯、v带、柑类、西瓜、葡萄、香蕉、草莓、板_、柿子、咖啡、葡萄酒{。  q有一些食物因吃v来酸Qh们就错误地把它们当成?jin)酸性食物,如山植、西U柿、醋{,其实q些东西正是典型?font style="BACKGROUND-COLOR: #ffff00">性食?/font>?</font><p></p><p><font size="2">多吃<font style="BACKGROUND-COLOR: #ffff00">性食?/font>。研I发玎ͼ多食<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q可保持血液呈q性,使得血液中乳酸、尿素等酸性物质减,q能防止其在壁上沉U,因而有软化血的作用Q故有hU?font style="BACKGROUND-COLOR: #ffff00">性食?/font>为“血液和血的清洁剂”。这里所说的酸碱性,不是食物本n的性质Q而是指食物经q消化吸收后Q留在体内元素的性质。常见的酸性元素有氮、碳、硫{;常见的碱性元素有钾、钠、钙、镁{。有的食物口呛_酸,如番茄、橘子,却都是地地道道的?font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q因为它们在体内代谢后的最l元素是钑օ素等?/font></p><p><font size="2">何谓酸性或<font style="BACKGROUND-COLOR: #ffff00">性食?/font>  所谓酸性食物或<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Qƈ不是指味道酸或咸的食物,而是指食物经q消化吸收和代谢后生的阳离子或阴离子占优势的食物。也是_(d)某种食物如经代谢后生的钾、钠、钙、镁{阳d占优势的则属<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q而代谢后产生氯、硫{阴d占优势的食物属酸性食物。柠檬、柑桔、杨桃等味道虽酸Q但它经代谢后,有机酸变成了(jin)水和二氧化碳Q后者经肺呼Z外,剩下的阳d占优势,仍属<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q同理,肉、鱼、蛋cdc面虽无酸味Q但代谢后生的阴离子较多,仍属于酸性食物。因此,不能从食物的味道来区分酸性或<font style="BACKGROUND-COLOR: #ffff00">性食?/font>?br /></font><br />看v来挺恐怖的。。。看来得注意下伙食了(jin)Q以后要多吃水果、蔬菜。。。~~~</p></font><img src ="http://www.shnenglu.com/firehutter/aggbug/14969.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/firehutter/" target="_blank">׃青菜的包?/a> 2006-11-10 16:09 <a href="http://www.shnenglu.com/firehutter/archive/2006/11/10/14969.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>q两天用C++Builder时碰C炚w题,关于TQuary?/title><link>http://www.shnenglu.com/firehutter/archive/2006/10/26/14255.html</link><dc:creator>׃青菜的包?/dc:creator><author>׃青菜的包?/author><pubDate>Thu, 26 Oct 2006 13:25:00 GMT</pubDate><guid>http://www.shnenglu.com/firehutter/archive/2006/10/26/14255.html</guid><wfw:comment>http://www.shnenglu.com/firehutter/comments/14255.html</wfw:comment><comments>http://www.shnenglu.com/firehutter/archive/2006/10/26/14255.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/firehutter/comments/commentRss/14255.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/firehutter/services/trackbacks/14255.html</trackback:ping><description><![CDATA[ <p>q几天拿到公总前项目中的一个用C++Builder做的E序Q在我机器上调试Q结果提C出错:(x)Operation not applicable</p> <p>使用断点跟踪之后发现错误出在使用TQuery时执行openҎ(gu)Ӟ回追Ҏ(gu)Q得Z下反馈:(x)<br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000">m_pqQuery</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">SQL</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">Add( (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Select (tng)* (tng)From (tng)Table1 (tng)Where (tng)I(yng)d (tng)= (tng):PId (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> (tng));<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />m_pqQuery</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">ParamByName( (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">PId</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> (tng))</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">AsInteger (tng) (tng) (tng) (tng) (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)某个int变量;</span> </div> <p> <br />执行完这句之后,按正常情况,参数PId应该被赋予了(jin)一个整形|但是Debug跟踪昄其D是跟未赋值前是同栯C的未知倹{所以在执行openҎ(gu)时出错了(jin)?/p> <p>如果我不是用该方法,直接把SQL语句写死Q?br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000">m_pqQuery</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">SQL</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">Add( (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Select (tng)* (tng)From (tng)Table1 (tng)Where (tng)I(yng)d (tng)= (tng)0 (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> (tng));</span> </div> <p> <br />q行正常</p> <p>或者用<br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #0000ff">char</span> <span style="COLOR: #000000"> (tng)sql[</span> <span style="COLOR: #000000">80</span> <span style="COLOR: #000000">];<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">int</span> <span style="COLOR: #000000"> (tng)n (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)某整形变量?<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />sprintf(sql,</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Select (tng)* (tng)From (tng)Table1 (tng)Where (tng)I(yng)d (tng)= (tng)%d</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,n);<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />pqQuery</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">SQL</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">Add(sql);</span> </div> <p> <br />也运行正?/p> <p>查了(jin)下,原来l参数赋值的Ҏ(gu)q没有错误,~译也能通过Q况且以前该E序肯定是能正常q行的。。真是奇怪ؓ(f)什么在我这里就赋不上倹{。调试了(jin)很久也没能找到原因,只好作ŞQؓ(f)?jin)顺利运行,只能改?f)sprintf和直接连接字W串的方式?br />在这里记上一W,希望以后能够扑ֈ原因。。?br /></p> <img src ="http://www.shnenglu.com/firehutter/aggbug/14255.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/firehutter/" target="_blank">׃青菜的包?/a> 2006-10-26 21:25 <a href="http://www.shnenglu.com/firehutter/archive/2006/10/26/14255.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++学习(fn)W记Q类与动态内存分配中各种问题的阶D|ȝhttp://www.shnenglu.com/firehutter/archive/2006/09/28/13111.html׃青菜的包?/dc:creator>׃青菜的包?/author>Thu, 28 Sep 2006 07:18:00 GMThttp://www.shnenglu.com/firehutter/archive/2006/09/28/13111.htmlhttp://www.shnenglu.com/firehutter/comments/13111.htmlhttp://www.shnenglu.com/firehutter/archive/2006/09/28/13111.html#Feedback1http://www.shnenglu.com/firehutter/comments/commentRss/13111.htmlhttp://www.shnenglu.com/firehutter/services/trackbacks/13111.html
  • 重蝲<<操作W?br />要重新定?lt;<操作W,以便它和cout一L(fng)来显C对象的内容Q请定义下面的友元操作符函数Q?
    ostream (tng)& (tng)operator (tng)<< (tng)(ostream (tng)& (tng)os,const (tng)c_name (tng)& (tng)obj)
    {
    os (tng)
    << (tng);// (tng)display (tng)object (tng)contents
    return (tng)os;
    }

    其中c_name是类名。如果该cL供了(jin)能够q回所需内容的公有方法,则可在操作符函数中用这些方法,q样便不?x)将他们讄为友元函C(jin)?br />
  • 转换函数
    要将单个D{换ؓ(f)cȝ型,需要创建原型如下所C的cL造函敎ͼ(x)
    c_name(type_name (tng)value);

    其中c_name为类名,type_name是要转换的类型的名称?br />要将c{换ؓ(f)其他cdQ需要创建原型如下所C的cL员函敎ͼ(x)
    operator (tng)type_name();

    虽然该函数没有声明返回类型,但应q回所需cd的倹{?br />使用转换函数时要心(j)。可以在声明构造函数时使用关键字explicitQ以防止它被用于隐式转换?br />
  • 其构造函C用new的类
    如果cM用new操作W来分配cL员指向的内存Q在设计时应采取一些预防措施?
    • 对于指向的内存是由new分配的所有类成员Q都应在cȝ析构函数中对其用deleteQ该操作W将释放分配的内存?
    • 如果析构函数通过Ҏ(gu)针类成员?sh)用delete来释攑ֆ存,则每个构造函数都应当使用new来初始化指针Q或它讄为空指针?
    • 构造函C要么使用new[]Q要么用newQ而不能؜用。如果构造函C用的是new[]Q则析构函数应用delete[]Q如果构造函C用的是newQ则析构函数应适用delete?
    • 应定义一个分配内存(而不是将指针指向已有内存Q的复制构造函数。这L(fng)序能够将cd象初始化为另一个对象。这U构造函数原型如下:(x)
      className(const (tng)className (tng)&)
    • 应定义一个重载赋值操作符的类成员函数Q其函数定义如下Q其中c_pointer是c_name的类成员Q类型ؓ(f)指向type_name的指针)(j)。下面的范例假设使用new[]来初始化变量c_pointerQ:(x)
      c_name (tng)& (tng)c_name::operator (tng)= (tng)(const (tng)c_name (tng)& (tng)cn)
      {
      if (tng)(this (tng)== (tng)& (tng)cn)
      return (tng)*this;

      delete (tng)[] (tng)c_pointer;
      // (tng)set (tng)size (tng)number (tng)of (tng)type_name (tng)units (tng)to (tng)be (tng)copyied
      c_pointer (tng)= (tng)new (tng)type_name[size];
      // (tng)then (tng)copy (tng)data (tng)pointed (tng)to (tng)by (tng)cn.c_pointer (tng)to
      // (tng)location (tng)pointed (tng)to (tng)by (tng)c_pointer

      return (tng)*this;
      }

    本文Ҏ(gu)前面几篇随笔中提到的问题?sh)作Z(jin)一个ȝQ感觉很有必要记下来?br />以上内容原文引用自参考书c中内容?br />
    参考书c:(x)C++PrimerPlus author:Stephen Prata



    ]]>
    C++学习(fn)W记Q关于C++cM动态内存分配中布局new操作W?/title><link>http://www.shnenglu.com/firehutter/archive/2006/09/28/13107.html</link><dc:creator>׃青菜的包?/dc:creator><author>׃青菜的包?/author><pubDate>Thu, 28 Sep 2006 06:42:00 GMT</pubDate><guid>http://www.shnenglu.com/firehutter/archive/2006/09/28/13107.html</guid><wfw:comment>http://www.shnenglu.com/firehutter/comments/13107.html</wfw:comment><comments>http://www.shnenglu.com/firehutter/archive/2006/09/28/13107.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.shnenglu.com/firehutter/comments/commentRss/13107.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/firehutter/services/trackbacks/13107.html</trackback:ping><description><![CDATA[ <p> (tng)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #0000ff">const</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">int</span> <span style="COLOR: #000000"> (tng)BUF (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #000000">512</span> <span style="COLOR: #000000"> (tng);<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">class</span> <span style="COLOR: #000000"> (tng)JustTesting<br /><img id="Codehighlighter1_41_157_Open_Image" onclick="this.style.display='none'; Codehighlighter1_41_157_Open_Text.style.display='none'; Codehighlighter1_41_157_Closed_Image.style.display='inline'; Codehighlighter1_41_157_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_41_157_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_41_157_Closed_Text.style.display='none'; Codehighlighter1_41_157_Open_Image.style.display='inline'; Codehighlighter1_41_157_Open_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span> <span id="Codehighlighter1_41_157_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.shnenglu.com/images/dot.gif" /> </span> <span id="Codehighlighter1_41_157_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)</span> <span style="COLOR: #0000ff">private</span> <span style="COLOR: #000000">:<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)JustTesting(</span> <span style="COLOR: #0000ff">const</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">string</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #000000">&</span> <span style="COLOR: #000000"> (tng)s (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Just (tng)Testing</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #0000ff">int</span> <span style="COLOR: #000000"> (tng)n (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">)<br /><img id="Codehighlighter1_114_118_Open_Image" onclick="this.style.display='none'; Codehighlighter1_114_118_Open_Text.style.display='none'; Codehighlighter1_114_118_Closed_Image.style.display='inline'; Codehighlighter1_114_118_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_114_118_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_114_118_Closed_Text.style.display='none'; Codehighlighter1_114_118_Open_Image.style.display='inline'; Codehighlighter1_114_118_Open_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)</span> <span id="Codehighlighter1_114_118_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.shnenglu.com/images/dot.gif" /> </span> <span id="Codehighlighter1_114_118_Open_Text"> <span style="COLOR: #000000">{<img src="http://www.shnenglu.com/images/dot.gif" />}</span> </span> <span style="COLOR: #000000"> (tng)<br /><img id="Codehighlighter1_137_141_Open_Image" onclick="this.style.display='none'; Codehighlighter1_137_141_Open_Text.style.display='none'; Codehighlighter1_137_141_Closed_Image.style.display='inline'; Codehighlighter1_137_141_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_137_141_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_137_141_Closed_Text.style.display='none'; Codehighlighter1_137_141_Open_Image.style.display='inline'; Codehighlighter1_137_141_Open_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)</span> <span style="COLOR: #000000">~</span> <span style="COLOR: #000000">JustTesting()</span> <span id="Codehighlighter1_137_141_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.shnenglu.com/images/dot.gif" /> </span> <span id="Codehighlighter1_137_141_Open_Text"> <span style="COLOR: #000000">{<img src="http://www.shnenglu.com/images/dot.gif" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)</span> <span style="COLOR: #0000ff">public</span> <span style="COLOR: #000000">:<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)<img src="http://www.shnenglu.com/images/dot.gif" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000">;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">int</span> <span style="COLOR: #000000"> (tng)main()<br /><img id="Codehighlighter1_172_565_Open_Image" onclick="this.style.display='none'; Codehighlighter1_172_565_Open_Text.style.display='none'; Codehighlighter1_172_565_Closed_Image.style.display='inline'; Codehighlighter1_172_565_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_172_565_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_172_565_Closed_Text.style.display='none'; Codehighlighter1_172_565_Open_Image.style.display='inline'; Codehighlighter1_172_565_Open_Text.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span> <span id="Codehighlighter1_172_565_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.shnenglu.com/images/dot.gif" /> </span> <span id="Codehighlighter1_172_565_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)</span> <span style="COLOR: #0000ff">char</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000"> (tng)buffer (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">char</span> <span style="COLOR: #000000"> (tng)[BUF];<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)JustTesting (tng)</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">pc1,</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">pc2,</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">pc3,</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">pc4;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)pc1 (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (tng)(buffer) (tng)JustTesting;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)pc2 (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (tng)JustTesting( (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> (tng)Heap1 (tng)</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> (tng), (tng)</span> <span style="COLOR: #000000">20</span> <span style="COLOR: #000000"> (tng));<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)pc3 (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (tng)(buffer (tng)</span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">sizeof</span> <span style="COLOR: #000000"> (tng)(JustTesting)) (tng)JustTesting(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Bad (tng)I(yng)dea</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">6</span> <span style="COLOR: #000000">); (tng) (tng)</span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000"> (tng)此处用一个JustTesging对象大小的偏U量避免pc3与pc1占用同一块内存,因ؓ(f)如果cd态的为其成员分配内存Q占用同一内存会(x)产生问题。?/span> <span style="COLOR: #008000"> <br /> <img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000"> (tng) (tng) (tng) (tng)pc4 (tng)</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (tng)JustTesting(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Heap2</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">10</span> <span style="COLOR: #000000">) (tng)<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)<img src="http://www.shnenglu.com/images/dot.gif" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)delete (tng)pc2;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)delete (tng)pc4;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)delete (tng)[] (tng)buffer;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> (tng) (tng) (tng) (tng)</span> <span style="COLOR: #0000ff">return</span> <span style="COLOR: #000000"> (tng)</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> </div> <p> <br />以上代码片断中,pc1和pc3为布局new操作W来分配内存Q而pc2和pc4为常规new操作W来分配内存 (tng)?br />对于常规new操作W分配的内存Q可以直接用:(x)<span style="COLOR: #000000"><span id="nf77f9b" class="Code"><span style="COLOR: #000000">delete (tng)pc2; </span></span></span>q样的语句操作来释放内存?br /><br />而对于布局new操作W分配的内存׃能这样做Q?span style="COLOR: #000000"><span id="frjfhlp" class="Code"><span style="COLOR: #000000">delete (tng)pc1;</span></span></span></p> <p>因ؓ(f)pc1和pc3q没有直接收到new操作W返回的地址Q而是由布局操作W指向了(jin)buffer的地址Qnew/deletepȝ知道已分配的512字节块bufferQ但对布局new操作W对该内存块做了(jin)何种处理一无所知?br />另一斚wQbuffer的地址是用<font color="#000000"><span id="7ljfbtr" class="Code">new []</span>初始化的Q因此必M?span class="Code">delete[]</span>而不?span class="Code">delete</span></font>?br /><font color="#ff0000">注意Q即使buffer是用new而不是new[]初始化的Q?span class="Code">delete pc1 </span>也将释放bufferQ而不是pc1?/font></p> <p>以上的代码确实释放了(jin)bufferQ?font color="#000000"><span id="1r99xpf" class="Code">delete [] buffer;</span><br /></font>但是由此产生?jin)新的问题,它没有?f)布局new操作W在该内存块中创建的对象调用析构函数Q我们只需要在析构函数中放入一D|C句就可以清楚的看刎ͼE序q没有销毁“JustTesting”和“Bad Idea”,也就是pc1和pc3指向的对象?br />那么q里需要我们显式的为布局new操作W创建的对象调用析构函数。正常情况下自动调用析构函敎ͼ<font color="#ff0000">q是需要显C用析构函数的数几种情况之一?/font><br />昑ּ调用析构函数Ӟ必须指定要销毁的对象。由于有指向对象的指针,因此可以q样写:(x)</p> <div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid"> <div> <span id="_162_532_Open_Text"> <span style="COLOR: #000000">pc3->~JustTesting();<br />pc1->~JustTesting();</span> </span> </div> </div> <p>把这D代码放?span class="Code">delete [] buffer;</span>之前Q这D늨序才完整无错?/p> <p>参考书c:(x)C++PrimerPlus author:Stephen Prata<br /></p> <img src ="http://www.shnenglu.com/firehutter/aggbug/13107.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/firehutter/" target="_blank">׃青菜的包?/a> 2006-09-28 14:42 <a href="http://www.shnenglu.com/firehutter/archive/2006/09/28/13107.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++学习(fn)W记Q在构造函C使用new时应注意的事?/title><link>http://www.shnenglu.com/firehutter/archive/2006/09/28/13106.html</link><dc:creator>׃青菜的包?/dc:creator><author>׃青菜的包?/author><pubDate>Thu, 28 Sep 2006 06:41:00 GMT</pubDate><guid>http://www.shnenglu.com/firehutter/archive/2006/09/28/13106.html</guid><wfw:comment>http://www.shnenglu.com/firehutter/comments/13106.html</wfw:comment><comments>http://www.shnenglu.com/firehutter/archive/2006/09/28/13106.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/firehutter/comments/commentRss/13106.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/firehutter/services/trackbacks/13106.html</trackback:ping><description><![CDATA[ <p>在用new来初始化对象的指针成员时必须特别心(j)Q以下是几点注意事项Q?/p> <ul> <li>如果在构造函C使用new来初始化指针成员Q则应在析构函数中用delete? </li> <li>new和delete必须怺兼容。new对应于deleteQnew[]对应于delete[]? </li> <li>如果有多个构造函敎ͼ则必M相同的方式用newQ要么都带中括号Q要么都不带。因为只有一个析构函敎ͼ因此所有的构造函数都必须与它兼容?font color="#ff0000">不过可以在一个构造函C使用new来初始化指针Q而在另外一个构造函C指针初始化为空QNull?Q,q是因ؓ(f)deleteQ无论是带中括号q是不带中括P(j)可以用于I指针?/font></li> </ul> <img src ="http://www.shnenglu.com/firehutter/aggbug/13106.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/firehutter/" target="_blank">׃青菜的包?/a> 2006-09-28 14:41 <a href="http://www.shnenglu.com/firehutter/archive/2006/09/28/13106.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++学习(fn)W记QC++cd动态内存分配中关于复制构造函数和赋值操作符http://www.shnenglu.com/firehutter/archive/2006/09/28/13103.html׃青菜的包?/dc:creator>׃青菜的包?/author>Thu, 28 Sep 2006 06:33:00 GMThttp://www.shnenglu.com/firehutter/archive/2006/09/28/13103.htmlhttp://www.shnenglu.com/firehutter/comments/13103.htmlhttp://www.shnenglu.com/firehutter/archive/2006/09/28/13103.html#Feedback0http://www.shnenglu.com/firehutter/comments/commentRss/13103.htmlhttp://www.shnenglu.com/firehutter/services/trackbacks/13103.html例如有以下classQ?/p>
    class  (tng)StringBad
    {
     (tng) (tng) (tng) (tng)
    private :
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
    char  (tng) *  (tng)str;
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
    int  (tng)len;
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
     (tng) (tng) (tng) (tng)
    public :
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)StringBad(
    const  (tng) char  (tng) *  (tng)s);
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)StringBad();
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
    ~ StringBad();
     (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
    }
    ;

    在构造函数和析构函数定义当中有如下定义:(x)

    StringBad::StringBad( const  (tng) char  (tng) *  (tng)s)
    {
     (tng) (tng) (tng) (tng)len (tng)
    =  (tng)std::strlen(s);
     (tng) (tng) (tng) (tng)str (tng)
    =  (tng) new  (tng) char  (tng)[len (tng) +  (tng) 1 ];
     (tng) (tng) (tng) (tng)
    }
     (tng)

    StringBad::StringBad()
    {
     (tng) (tng) (tng) (tng)len (tng)
    =  (tng) 4  (tng);
     (tng) (tng) (tng) (tng)str (tng)
    =  (tng) new  (tng) char [ 4 ];
     (tng) (tng) (tng) (tng)
    }
     (tng)

    StringBad::
    ~ StringBad()
    {
     (tng) (tng) (tng) (tng)
     (tng) (tng) (tng) (tng)delete (tng)[] (tng)str;
    }

    那么在程序当中如果有以下代码Q?/p>

    StringBad (tng)sports( " Spinach (tng)Leaves (tng)Bow1 (tng)for (tng)bollars " );
    StringBad (tng)sailor (tng)
    =  (tng)sports;

    以上的第二条初始化语句将?x)调用什么构造函敎ͼCQ这UŞ式的初始化等效于下面的语句:(x)

    StringBad (tng)sailor (tng) =  (tng)StringBad(sports);

    因ؓ(f)sports的类型ؓ(f)StringBadQ因此相应的构造函数原型应该如下:(x)

    StringBad( const  (tng)StringBad (tng) & );

    当我们用一个对象来初始化另一个对象时Q编译器自动生成上q构造函敎ͼUCؓ(f)复制构造函敎ͼ因ؓ(f)它创建对象的一个副本)(j)?br />现在我们不妨ȝ一下所谓的隐式成员函数Q即C++自动提供?jin)以下这些成员函敎ͼ?x)

    • 默认构造函敎ͼ如果没有定义构造函数?
    • 复制构造函敎ͼ如果没有定义?
    • 赋值操作符Q如果没有定义?
    • 默认析构函数Q如果没有定义?
    • 地址操作W,如果没有定义?

    现在我们来看看我们没有定义复制构造函数的情况下调用隐式复制构造函数将?x)出C么情c(din)?br />从构造函数定义的代码片断可以看到Q当中用new操作W初始化?jin)一个指针strQ而隐式的复制构造函数是按D行复制的Q那么对于指针strQ将?x)进行如下复Ӟ?x)

    sailor.str (tng) =  (tng)sports.str;

    q里复制的不是字W串Q而是一个指向字W串的指针!也就是说Q我们将得到两个指向同一个字W串的指针!由此?x)生的问题不a而喻。当其中一个对象调用了(jin)析构函数之后Q其str指向的内存将被释放,q个时候我们如果调用另一个对象,其str指向的地址数据?x)是什么?很明昑ְ?x)出C可预料的l果?/p>

    所以由此可见,如果cM包含?jin)用new初始化的指针成员Q应当定义一个复制构造函敎ͼ以复制指向的数据Q而不是指针,q被UCؓ(f)深度复制。因为默认的复Ӟ或成为成员复Ӟ(j)仅浅的赋值指针信息?/strong>

    我们再看以下代码片断Q我们稍做修改:(x)

    StringBad (tng)headline1( " Celery (tng)Stalks (tng)at (tng)Midnight " );
    StringBad (tng)knot;
    knot (tng)
    =  (tng)headline1;

    q里的最后一行将与以上例子有所区别Q现在是已有对象赋l另一个已有对象,q将?x)采取其他操作,即用重载的赋值操作符。(我们需要知道的是:(x)初始化L?x)调用复制构造函敎ͼ而?操作W时也可能调用赋值操作符Q因为C++允许对象赋|q是通过自动为类重蝲赋值操作符实现的。其原型如下Q?/p>

    Class_name (tng) &  (tng)Class_name:: operator  (tng) =  (tng)( const  (tng)Class_name (tng) & );

    它接受ƈq回一个指向类对象的引用?br />与隐式的复制构造函CP隐式的对象赋值操作符也会(x)产生同样的问题,卛_含了(jin)使用new初始化的指针成员Ӟ只会(x)采用复制。所以我们需要用同L(fng)解决办法Q?strong>卛_义一个重载的赋值操作符来实现深度复制?/strong>

    所以综上所qͼ如果cM包含?jin)用new初始化的指针成员Q我们应该显式定义一个复制构造函数和一个重载的赋值操作符来实现其深度复制Q避免由此带来的成员复制问题

    参考书c:(x)C++PrimerPlus author:Stephen Prata



    ]]>
    关于C++学习(fn)W记http://www.shnenglu.com/firehutter/archive/2006/09/28/13102.html׃青菜的包?/dc:creator>׃青菜的包?/author>Thu, 28 Sep 2006 06:23:00 GMThttp://www.shnenglu.com/firehutter/archive/2006/09/28/13102.htmlhttp://www.shnenglu.com/firehutter/comments/13102.htmlhttp://www.shnenglu.com/firehutter/archive/2006/09/28/13102.html#Feedback1http://www.shnenglu.com/firehutter/comments/commentRss/13102.htmlhttp://www.shnenglu.com/firehutter/services/trackbacks/13102.htmlW记中参考了(jin)一些书c中的范例和一些说明。都是自己学?fn)过E中整理出来的,主要目的是给自己日后参考?img src ="http://www.shnenglu.com/firehutter/aggbug/13102.html" width = "1" height = "1" />

    ]]>
    ƷȾþþø| þۺϺݺۺϾþۺ88 | ڵСþþþþþ | պŷһþþþ | ?VþþƷ| ˺ݺۺ88ۺϾþ| ޷AVþò| AVۺϾþ| ͵þþþƷר| Ʒ˿þþþþò| ҹҹݺݾþAV| Ļһþ| þŮվ| ɫ͵͵͵þ˴ý| һƷþ| þһ | þþþһ| 999þþѾƷ| ޾Ʒþþþþ| yellowĻþ| ˾ƷѾþþþ| þþƷѹۿͬ| ˾Ʒþ| ˾þþƷһ| ޺ݺۺϾþþþ| ˾þۺӰԺ| ޹ƷۺϾþһ| ݺɫ˾þþƷۺ| AëƬþþƷ| 94þù׾Ʒ| ɫۺϾþۺۿ| ȾþֻоƷ| þþƷƷapp| 777þµַ| Ʒþþþþ| 99þþþƷѹۿ| þ99ƷþֻоƷ| þ91Ʒ91þ鶹| Ʒþ| ɫ͵͵888ŷƷþþ| ݺ޾þþþþۺ|