⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 effective c++ 2e item7.htm

📁 Effective-c++.国外很经典的一本关于c++编程的电子书。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
NewHandlerSupport&lt;T&gt;::set_new_handler(new_handler p)<BR>{<BR>&nbsp; 
new_handler oldHandler = currentHandler;<BR>&nbsp; currentHandler = p;<BR>&nbsp; 
return oldHandler;<BR>}</P>
<P>template&lt;class T&gt;<BR>void * NewHandlerSupport&lt;T&gt;::operator 
new(size_t size)<BR>{<BR>&nbsp; new_handler globalHandler 
=<BR>&nbsp;&nbsp;&nbsp; std::set_new_handler(currentHandler);</P>
<P>&nbsp; void *memory;</P>
<P>&nbsp; try {<BR>&nbsp;&nbsp;&nbsp; memory = ::operator new(size);<BR>&nbsp; 
}<BR>&nbsp; catch (std::bad_alloc&amp;) {<BR>&nbsp;&nbsp;&nbsp; 
std::set_new_handler(globalHandler);<BR>&nbsp;&nbsp;&nbsp; throw;<BR>&nbsp; 
}</P>
<P>&nbsp; std::set_new_handler(globalHandler);</P>
<P>&nbsp; return memory;<BR>}</P>
<P>// this sets each currentHandler to 0<BR>template&lt;class 
T&gt;<BR>new_handler NewHandlerSupport&lt;T&gt;::currentHandler;</P>
<P>有了这个模板类,对类X加上set_new_handler功能就很简单了:只要让X从newHandlerSupport&lt;X&gt;继承:<BR>// 
note inheritance from mixin base class template. (See<BR>// my article on 
counting objects for information on why<BR>// private inheritance might be 
preferable here.)<BR>class X: public NewHandlerSupport&lt;X&gt; {</P>
<P>&nbsp; 
...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// as before, but no declarations 
for<BR>};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// set_new_handler or operator new</P>
<P><BR>使用X的时候依然不用理会它幕后在做些什么;老代码依然工作。这很好!那些你常不去理会的东西往往是最可信赖的。</P>
<P>使用set_new_handler是处理内存不够情况下一种方便,简单的方法。这比把每个new都包装在try模块里当然好多了。而且,NewHandlerSupport这样的模板使得向任何类增加一个特定的new-handler变得更简单。“混合风格”的继承不可避免地将话题引入到多继承上去,在转到这个话题前,你一定要先阅读条款43。</P>
<P>1993年前,C++一直要求在内存分配失败时operator new要返回0,现在则是要求operator 
new抛出std::bad_alloc异常。很多C++程序是在编译器开始支持新规范前写的。C++标准委员会不想放弃那些已有的遵循返回0规范的代码,所以他们提供了另外形式的operator 
new(以及operator 
new[]——见条款8)以继续提供返回0功能。这些形式被称为“无抛出”,因为他们没用过一个throw,而是在使用new的入口点采用了nothrow对象:<BR>class 
Widget { ... };</P>
<P>Widget *pw1 = new Widget;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 
分配失败抛出std::bad_alloc if</P>
<P>if (pw1 == 0) 
...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 这个检查一定失败<BR>Widget *pw2 =<BR>&nbsp; new (nothrow) 
Widget;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 若分配失败返回0</P>
<P>if (pw2 == 0) 
...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 这个检查可能会成功</P>
<P>不管是用“正规”(即抛出异常)形式的new还是“无抛出”形式的new,重要的是你必须为内存分配失败做好准备。最简单的方法是使用set_new_handler,因为它对两种形式都有用。</P><BR><BR></DIV></DIV></DIV><BR><BR>
<SCRIPT src="Effective C++ 2e Item7.files/get_readnum.htm"></SCRIPT>

<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666 
border=0>
  <TBODY>
  <TR>
    <TH id=white bgColor=#990000><FONT 
  color=#ffffff>对该文的评论</FONT></TH></TR></TBODY></TABLE><BR>
<SCRIPT language=javascript>
<!--
function isEmpty(s)
{  
	return ((s == null) || (s.length == 0))
}
function submit1()
{
   if (isEmpty(document.add_critique.csdnname.value) || isEmpty(document.add_critique.csdnpassword.value) || isEmpty(document.add_critique.critique_content.value))
   {
      alert('登陆名,密码,评论不能为空!!!!')   ;
      return false;
   }
   document.add_critique.submit();
 }
//-->
</SCRIPT>

<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666 
border=0>
  <TBODY>
  <TR>
    <TH id=white bgColor=#990000><FONT 
color=#ffffff>发表评论</FONT></TH></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#ffffff 
border=0>
  <TBODY>
  <TR>
    <TD>
      <FORM name=add_critique action=/develop/add_critique.asp 
      method=post><INPUT type=hidden value=add name=critique_add> <INPUT 
      type=hidden value=8610 name=from> &nbsp;&nbsp;评论人: <INPUT name=csdnname> 
      &nbsp;&nbsp;密码: <INPUT type=password name=csdnpassword> 
      &nbsp;&nbsp;评论:<BR>&nbsp;&nbsp; <TEXTAREA name=critique_content rows=8 cols=100></TEXTAREA><BR>&nbsp;&nbsp; 
<INPUT onclick=javascript:submit1(); type=button value=发表评论 name=ubmit> 
      <INPUT type=hidden value=8610 name=id> </FORM></TD></TR></TBODY></TABLE>
<TABLE width=770 border=0>
  <TBODY>
  <TR>
    <TD>
      <TABLE cellPadding=2 width=770 border=0>
        <TBODY>
        <TR>
          <TD height=10></TD></TR>
        <TR>
          <TD align=left width=130><!--Adforward Begin:--><IFRAME 
            marginWidth=0 marginHeight=0 
            src="Effective C++ 2e Item7.files/adfshow.htm" frameBorder=0 
            width=120 scrolling=no 
            height=60>
<SCRIPT LANGUAGE="JavaScript1.1" SRC="http://ok100.allyes.com/main/adfshow?user=CP_CSDNButton|CG_Button|C_BottomButton&db=ok100&local=yes&js=on">
</SCRIPT>
<NOSCRIPT>
<A HREF=http://ok100.allyes.com/main/adfclick?user=CP_CSDNButton|CG_Button|C_BottomButton&db=ok100>
<IMG SRC=http://ok100.allyes.com/main/adfshow?user=CP_CSDNButton|CG_Button|C_BottomButton&db=ok100 WIDTH=120 HEIGHT=60 BORDER=0></a>
</NOSCRIPT>
</IFRAME><!--Adforward End--></TD>
          <TD align=middle width=510>
            <OBJECT id=Movie1 
            codeBase=http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0 
            height=60 width=468 
            classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000><PARAM NAME="movie" VALUE="http://www.csdn.net/images/ad/csdn_media.swf"><PARAM NAME="quality" VALUE="high">
               <EMBED src="http://www.csdn.net/images/ad/csdn_media.swf" 
            quality=high WIDTH=468 HEIGHT=60 
            TYPE="application/x-shockwave-flash" 
            PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"> 
            </EMBED> </OBJECT></TD>
          <TD align=middle width=130><A 
            href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG 
            src="http://www.csdn.net/images/biaoshi.gif" border=0></A> 
        </TD></TR></TBODY></TABLE></TD></TR>
  <TR>
    <TD>
      <TABLE height=20 cellSpacing=0 cellPadding=0 width=770 align=default 
      bgColor=#1f60a0 border=0>
        <TBODY>
        <TR class=font13px vAlign=center align=middle>
          <TD class=font13px vAlign=center width=90 height=2></TD>
          <TD width=2 rowSpan=2><IMG height=13 
            src="http://www.csdn.net/images/ad4.gif" width=2></TD>
          <TD class=font13px width=90 height=2></TD>
          <TD width=3 rowSpan=2><FONT color=#ffffff><IMG height=13 
            src="http://www.csdn.net/images/ad4.gif" width=2></FONT></TD>
          <TD class=font13px width=90 height=2></TD>
          <TD width=2 rowSpan=2><IMG height=13 
            src="http://www.csdn.net/images/ad4.gif" width=2></TD>
          <TD class=font13px width=90 height=2></TD>
          <TD width=3 rowSpan=2><FONT color=#ffffff><IMG height=13 
            src="http://www.csdn.net/images/ad4.gif" width=2></FONT></TD>
          <TD width=350 height=2><FONT color=#ffffff></FONT></TD>
          <TD width=10 rowSpan=2><FONT color=#ffffff><IMG height=11 
            src="http://www.csdn.net/images/ad3.gif" width=6></FONT></TD>
          <TD class=font13px width=60 height=2></TD></TR>
        <TR class=font14px vAlign=center align=middle>
          <TD class=font13px vAlign=center width=90><A 
            href="http://www.csdn.net/intro/intro.shtm"><FONT 
            color=#ffffff>美达美简介</FONT></A></TD>
          <TD class=font13px width=90><A 
            href="http://www.csdn.net/intro/ad.shtm"><FONT 
            color=#ffffff>广告服务</FONT></A></TD>
          <TD class=font13px width=90><A 
            href="http://www.csdn.net/English/"><FONT 
            color=#ffffff>英语步步高</FONT></A></TD>
          <TD class=font13px width=90><A href="http://www.csdn.net/dev/"><FONT 
            color=#ffffff>程序员大本营</FONT></A></TD>
          <TD align=right width=350><SPAN class=font13px><A 
            href="mailto:webmaster@csdn.net"><FONT 
            color=#ffffff>百联美达美科技有限公司</FONT></A></SPAN><FONT color=#ffffff><SPAN 
            class=font13px> </SPAN></FONT></TD>
          <TD class=font13px width=60><FONT color=#ffffff>版权所有</FONT>
            <SCRIPT>document.write("<img src=http://202.106.156.10/stat.asp?user=designol&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" width=0 height=0 border=0>");</SCRIPT>
          </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -