??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久成人精品无码,亚洲国产精品无码久久久蜜芽,亚洲v国产v天堂a无码久久http://www.shnenglu.com/firehutter/雕栏玉砌应犹在,只是朱颜?/description>zh-cnThu, 08 May 2025 19:47:39 GMTThu, 08 May 2025 19:47:39 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++学习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因为它让程序能够选择为特定类型设计的ҎQ这样问题就来了Q既然动态联~这么好Qؓ什么还要设计两U类型的联编Qؓ什么默认的联编Ҏ是静态的而不是动态?

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

]]>
得注意下伙食了。?/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全面检查了下n体,拿到体检报告的时候,发现有一血液尿酸偏高,ȝ在后面批注,食动物内脏?br /><br />疑惑了一下,正准备网上查查,l果有同事发了封公司邮gQ正好是说这个事的,貌似出现该情늚Z有不。看了看内容Q如下:<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以及面_、大c뀁花生、大麦、啤酒等。所以不要误以ؓҎ{吃h很酸是酸性食物,那是大错牚w的,刚好相反Q苹果是一U碱性的食物?br /><br /></font><font color="#000080" size="2">酸性体质,健康新杀?br /><br />   吃得好了,w体却变 "??br />   "Z那年?~饭又少?如今q年?吃得真不?的确如此,C人生zLq不断在提高,每天大鱼大肉,K都离不开?q轻人去麦当功뀁肯德基吃洋快餐更是家常侉K.可是Ҏ一调查研I表?生活水^来好,ȝ水^来高,C文明病却来多;发病率越来越?患者越来越q轻化。有数字昄:我国目前的高血压h已辑ֈ1.6 个亿Q我国的高血脂h是 6500万,p尿病h 4800万ƈ以每q?0%的速度高速增长,每年M癌症的有130 多万人,M心脑血疾病的?00 多万人?0q代我们国家呼吁要让高血压低_可现在我们在高血压面前低了头。我国心脏病的发病年龄比80 q代初期整整提早?5q?也就是说原来50岁开始得心脏病的,现在35岁的人就开始得心脏病。多么触目惊心的数字Q而这些数字只是官方统?q没有加上未查出来的。我们不要?q到底是怎么?来多的医学家、营d家和研究机构为此现象讄Nq行研究Q最后得Z致结?酸性体质是病魔元凶?br />    挖出酸性体质的?br />    那么酸性体质是怎么来的?Cd研究向我们揭CZ真相:D蛋油米面等q些高蛋白、高脂肪、高p类食在ؓ我们提供能量和营ȝ同时Q也在提供大量的酸性物质。那么到底什么物质生什么酸? 我们一h看看: 蛋白?主要来源于瘦肉、鱼、蛋{?在体内代谢生含}基酸、含h}基酸、尿酸等{?脂肪(主要来源于肥肉、a{?在体内代谢生烃丁酸、乙C?也称酮酸);x化合?p类Q主要来源于c面)在体内代谢生甘沚w、丙酮酸、^酸?br />    "?不除Q后患无I?br />    Z摄入的酸性食物过多,过了h体酸^衡的调节能力Q肝肄w体重要部位负药作,Z的酸^衡就会被破坏Q体内酸碱pQ甚臛_现轻度酸中毒反应Q这是我们所说的酸性体质?br />    其实我们Zp个养鱼的池塘Q占Z体重70%的体液就是池塘里的水Q水被污染了鱼就要生病死亡,同样的道理,Z体液被酸性物质污染了Q组成我们h体的60万亿个细胞的生态环境就会发生变化,l胞׃发生H变和病变而媄响我们的健康。我们都知道:酸性物质沉U在肝脏中会产生脂肪?酸性物质沉U在肾脏中会影响肄排泄功能QɞK偏高而生痛?酸性物质沉U在兌或组l器宫内引v相应炎症Q导致关节炎{疾?酸性物质沉U在胰腺中,会媄响胰岛细胞的zL、媄响胰岛素的敏感性引发糖病及其q发症等{。更Z重的是酸性物质沉U在血液里、血壁使血壁增厚Q引起动脉硬化,引发中风、心肌梗塞等各种心脑血疾病?br /><br />………?br /></font><font size="3"><br /><font size="2">含钠、钾、钙、镁{金属元素较多的乃是性食品。ƈ非味道酸的就是酸味食品,应该掌握一点:所有酸味的水果、豆制品都ؓ<font style="BACKGROUND-COLOR: #ffff00">性食?/font>而不是酸性食物,<font style="BACKGROUND-COLOR: #ffff00">性食?/font>被认为是h容作用的食物?br />豆腐、牛奶、芹菜、土豆、竹W、香菇、胡萝卜、v带、绿豆、香蕉、西瓜、草莓等及大部分的蔬菜、水果都是碱性的?br />食物的“酸性”会影响体态和皮肤健美?br /> l测定,q性的食物有:豆腐、豌豆、大豆、绿豆、a菜、芹菜、番薯、莲藕、洋葱、茄子、南瓜、黄瓜、蘑菇、萝卜、牛奶等。而呈性的食物有:菠菜、白菜、卷心菜、生菜、胡萝卜、竹W、马铃薯、v带、柑类、西瓜、葡萄、香蕉、草莓、板_、柿子、咖啡、葡萄酒{。  q有一些食物因吃v来酸Qh们就错误地把它们当成了酸性食物,如山植、西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占优势的则属<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q而代谢后产生氯、硫{阴d占优势的食物属酸性食物。柠檬、柑桔、杨桃等味道虽酸Q但它经代谢后,有机酸变成了水和二氧化碳Q后者经肺呼Z外,剩下的阳d占优势,仍属<font style="BACKGROUND-COLOR: #ffff00">性食?/font>Q同理,肉、鱼、蛋cdc面虽无酸味Q但代谢后生的阴离子较多,仍属于酸性食物。因此,不能从食物的味道来区分酸性或<font style="BACKGROUND-COLOR: #ffff00">性食?/font>?br /></font><br />看v来挺恐怖的。。。看来得注意下伙食了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出错:Operation not applicable</p> <p>使用断点跟踪之后发现错误出在使用TQuery时执行openҎӞ回追ҎQ得Z下反馈:<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( </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Select * From Table1 Where Id = :PId </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> );<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( </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">PId</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> )</span> <span style="COLOR: #000000">-></span> <span style="COLOR: #000000">AsInteger     </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> 某个int变量;</span> </div> <p> <br />执行完这句之后,按正常情况,参数PId应该被赋予了一个整形|但是Debug跟踪昄其D是跟未赋值前是同栯C的未知倹{所以在执行openҎ时出错了?/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( </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Select * From Table1 Where Id = 0 </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> );</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"> 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"> n </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> 某整形变量?<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 * From Table1 Where Id = %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>查了下,原来l参数赋值的Ҏq没有错误,~译也能通过Q况且以前该E序肯定是能正常q行的。。真是奇怪ؓ什么在我这里就赋不上倹{。调试了很久也没能找到原因,只好作ŞQؓ了顺利运行,只能改ؓ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++学习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来显C对象的内容Q请定义下面的友元操作符函数Q?
    ostream & operator << (ostream & os,const c_name & obj)
    {
    os 
    << ;// display object contents
    return os;
    }

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

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

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

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

      return *this;
      }

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



    ]]>
    C++学习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> </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"> </span> <span style="COLOR: #0000ff">int</span> <span style="COLOR: #000000"> BUF </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">512</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">class</span> <span style="COLOR: #000000"> 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" />    </span> <span style="COLOR: #0000ff">private</span> <span style="COLOR: #000000">:<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" />        JustTesting(</span> <span style="COLOR: #0000ff">const</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">string</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">&</span> <span style="COLOR: #000000"> s </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Just Testing</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #0000ff">int</span> <span style="COLOR: #000000"> n </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </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" />        </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"> <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" />        </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" />    </span> <span style="COLOR: #0000ff">public</span> <span style="COLOR: #000000">:<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" />    <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"> 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" />    </span> <span style="COLOR: #0000ff">char</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000"> buffer </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">char</span> <span style="COLOR: #000000"> [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" />    JustTesting </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" />    pc1 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (buffer) JustTesting;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" />    pc2 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> JustTesting( </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> Heap1 </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> , </span> <span style="COLOR: #000000">20</span> <span style="COLOR: #000000"> );<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" />    pc3 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> (buffer </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">sizeof</span> <span style="COLOR: #000000"> (JustTesting)) JustTesting(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Bad Idea</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">6</span> <span style="COLOR: #000000">);  </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000"> 此处用一个JustTesging对象大小的偏U量避免pc3与pc1占用同一块内存,因ؓ如果cd态的为其成员分配内存Q占用同一内存会产生问题。?/span> <span style="COLOR: #008000"> <br /> <img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">    pc4 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> 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">) <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" />    <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" />    delete pc2;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" />    delete pc4;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" />    delete [] buffer;<br /><img src="http://www.shnenglu.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">return</span> <span style="COLOR: #000000"> </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来分配内存 ?br />对于常规new操作W分配的内存Q可以直接用:<span style="COLOR: #000000"><span id="1lx5xvn" class="Code"><span style="COLOR: #000000">delete pc2; </span></span></span>q样的语句操作来释放内存?br /><br />而对于布局new操作W分配的内存׃能这样做Q?span style="COLOR: #000000"><span id="jdtr3rv" class="Code"><span style="COLOR: #000000">delete pc1;</span></span></span></p> <p>因ؓpc1和pc3q没有直接收到new操作W返回的地址Q而是由布局操作W指向了buffer的地址Qnew/deletepȝ知道已分配的512字节块bufferQ但对布局new操作W对该内存块做了何种处理一无所知?br />另一斚wQbuffer的地址是用<font color="#000000"><span id="jnd3f35" 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>以上的代码确实释放了bufferQ?font color="#000000"><span id="dljppr5" class="Code">delete [] buffer;</span><br /></font>但是由此产生了新的问题,它没有ؓ布局new操作W在该内存块中创建的对象调用析构函数Q我们只需要在析构函数中放入一D|C句就可以清楚的看刎ͼE序q没有销毁“JustTesting”和“Bad Idea”,也就是pc1和pc3指向的对象?br />那么q里需要我们显式的为布局new操作W创建的对象调用析构函数。正常情况下自动调用析构函敎ͼ<font color="#ff0000">q是需要显C用析构函数的数几种情况之一?/font><br />昑ּ调用析构函数Ӟ必须指定要销毁的对象。由于有指向对象的指针,因此可以q样写:</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: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++学习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来初始化对象的指针成员时必须特别心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是因ؓdeleteQ无论是带中括号q是不带中括P可以用于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++学习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  StringBad
    {
        
    private :
            
    char   *  str;
            
    int  len;
             
        
    public :
            StringBad(
    const   char   *  s);
            StringBad();
            
    ~ StringBad();
            
    }
    ;

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

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

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

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

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

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

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

    StringBad sailor  =  StringBad(sports);

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

    StringBad( const  StringBad  & );

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

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

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

    sailor.str  =  sports.str;

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

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

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

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

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

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

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

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

    参考书c:C++PrimerPlus author:Stephen Prata



    ]]>
    关于C++学习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记中参考了一些书c中的范例和一些说明。都是自己学习过E中整理出来的,主要目的是给自己日后参考?img src ="http://www.shnenglu.com/firehutter/aggbug/13102.html" width = "1" height = "1" />

    ]]>
    ɫۺϾþ| պһþþþþ| Ʒþþþ龫Ʒ| 97þþþ| þ| vaþþþ| þˬˬƬav鷳| avպƷþþþþa| ˾þü91| ŷսպ91ۺһþþ| ˾þóۺӰԺ | ݺݸɺݺݾþ| þñۺϾþ| þavרavһ| þۺϺݺۺϾþü | þ99ֻоƷ| ƷŷƬþùŷ... ƷŷƬþùŷ | ޹Ʒþ| þùһ| þþþ޾Ʒ| ҹƵþþþһ| þþþþþۺձ| Ʒþþþþþþ| þùƷһ| Ů㽶þ**ëƬƷ| պavþþƷ| ԭۺϾþô| þav뾫Ʒ˳| ޹þ| ŷ˾þƬ| þ99þ99Ʒӿ| þþƷ99Ʒ| 㽶aaþëƬ | þëƬһ| ˼˼þ99ѾƷ6| պݺݾþ͵͵ɫۺ96| þþþùƷ| þ99þëƬһ| ŮһaëƬþw| 99Ʒ˾þþô߽| 91Ʒþþþþ|