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

📄 6847.htm

📁 C++细节解释
💻 HTM
字号:
<HTML>
<HEAD>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>


<style >
.fst{padding:0px 15px;width:770px;border-left:0px solid #000000;border-right:0px solid #000000}
.fstdiv3 img{border:0px;border-right:8px solid #eeeecc;border-top:6px solid #eeeecc}
</style>
<title>
Guru of the week:#36 初始化.
</title>
</HEAD>
<BODY>
<center>

<div align=center><div class=fst align=left><div class=fstdiv3 id=print2>
<font color="#000000" size="4"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
Guru of the week:#36 初始化.</b></font><P>/*此文是译者出于自娱翻译的GotW(Guru of the Week)系列文章的一篇,原文的版权是属于Hub Sutter(著名的C++专家,《Exceptional C++》的作者)。此文的翻译没有征得原作者的同意,只供学习讨论。——译者:黄森堂*/</P>  
<P><FONT color=#0000ff size=4><B>#36 初始化.</B></FONT></P>  
<P><B>难度:</B>3/10</P>  
<P>直接初始化与拷贝初始化有什么区别呢?,且在什么时候使用?</P>  
<P><B>JG </B><B>问题:</B></P>  
<P><B><FONT size=4>1.</FONT></B>直接初始化与拷贝初始化有什么区别呢?</P>  
<P>(提示:查看早期的GotW)</P>  
<H4>Guru 问题</H4>  
<P>2.以下哪处使用直接初始化且何处使用拷贝初始化?</P><PRE>  struct T : S {
    T() : S(1),             // 初始化基类
          x(2) {}           // 成员函数初始化
    X x;
  };

  T f( T t ) {              // passing a function argument
    return t;               // 返回值
  }

  S s;
  T t;
  S&amp; r = t;
  reinterpret_cast&lt;S&gt;(t);   // 执行reinterpret_cast
  static_cast&lt;S&gt;(t);        // 执行static_cast
  dynamic_cast&lt;T&amp;&gt;(r);      // 执行dynamic_cast
  const_cast&lt;const T&amp;&gt;(t);  // 执行const_cast

  try {
    throw T();              // 抛出异常
  } catch( T t ) {          // 处理异常
  }

  f( T(s) );                // functional-notation type conversion
  S a[3] = { 1, 2, 3 };     // brace-enclosed initializers
  S* p = new S(4);          // new expression<!--<IMG border=0 height=2  
      src="GotW #36 Initialization.files/h-line.gif" width=248>-->
</PRE>  
<P><B>解决方法:</B></P>  
<P><FONT color=#999933><B><FONT size=4>1.</FONT></B>直接初始化与拷贝初始化有什么区别呢?</FONT></P>  
<P><FONT color=#999933>(提示:查看早期的GotW)</FONT></P>  
<P>直接初始化的理由:对象在初始化使用专一构造函数(可以转换),同等于"T t(u);":</P><PRE>    U u;
    T t1(u); // 调用 T::T( U&amp; ) 或类似的</PRE>  
<P>拷贝初始化的理由:对象在初始化使用拷贝构造函数之后,如果成功的话首先调用用户预定的转换函数,同等于"T t = u;":</P><PRE>    T t2 = t1;  // 相似类型: calls T::T( T&amp; ) 或类似的
    T t3 = u;   // 不同类型different type: calls T::T( T(u) )
                //  or T::T( u.operator T() ) or similar</PRE>  
<P>[旁注:上述的"类似的"理由是拷贝与转换构造从普通的引用中取对象的细微差异(引用可能是const或volatile或两者),与取得用户定义的转换构造或操作,或返回对象胜于引用</P>  
<P>注释:在最后的情形("T t3 = u;"),编译器调用两个用户定义的转换(创建临时对象)与T的拷贝构造(从临时对象构造t3),或者它选择取消从临时对象构造,而是直接从u构造t3(在结果前同等于"T t3(u);"),因为在1997年7月的最后草案里,规定编译器在取消临时对象的范围是有受限,但在优化里与在返回值的优化里是允许的,更多详细的信息,请看GotW #1(基础课)与GotW #27</P>  
<H4><FONT color=#999933>Guru 问题</FONT></H4>  
<P><B><FONT color=#999933>2.以下哪处使用直接初始化且何处使用拷贝初始化?</FONT></B></P>  
<P>Section 8.5 [dcl.init] covers most of these. There were also three tricks that actually don't involve initialization at all... did you catch them?</P><PRE><FONT color=#999933>  struct T : S {
    T() : S(1),             // 基类初始化
          x(2) {}           // 内部成员始化
    X x;
  };</FONT></PRE>  
<P>基类与内部成员使用直接初始化。</P><PRE><FONT color=#999933>  T f( T t ) {              // passing a function argument
    return t;               // 返回值
  }</FONT></PRE>  
<P>传递到函数的对象与返回值使用拷贝初始化。</P><PRE><FONT color=#999933>  S s;
  T t;
  S&amp; r = t;
  reinterpret_cast&lt;S&gt;(t);   // 抛行reinterpret_cast</FONT></PRE>  
<P>窍门:A reinterpret_cast在所有的new出来的对象都不能初始化,但reinterpret从S仅仅是按位拷贝。</P><PRE><FONT color=#999933>  static_cast&lt;S&gt;(t);        // 抛行static_cast</FONT></PRE>  
<P>static_cast使用直接初始化。</P><PRE><FONT color=#999933>  dynamic_cast&lt;T&amp;&gt;(r);      // 抛行dynamic_cast
  const_cast&lt;const T&amp;&gt;(t);  // 抛行const_cast</FONT></PRE>  
<P>窍门:new出来的对象没有初始化。</P><PRE><FONT color=#999933>  try {
    throw T();              // 抛出异常
  } catch( T t ) {          // 处理异常
  }</FONT></PRE>  
<P>抛出与捕获异常同使用拷贝初始化。</P>  
<P>注释:在部分代码里有两个拷贝对象,共有3个T对象:在异常点内产生对象并拷贝抛出,与在这种情形有二个在产生的对象进行拷贝,因为捕获处理抛出对象的值(也就是说这里的捕获处理是按值传送的。)</P><PRE><FONT color=#999933>  f( T(s) );                // </FONT><FONT color=#999933>函数参数类型转换</FONT></PRE>  
<P>“构造语法”中的类型转换使用直接初始化</P><PRE><FONT color=#999933>  S a[3] = { 1, 2, 3 };     // </FONT><FONT color=#999933>对象数组的初始化</FONT></PRE>  
<P>对象数组的初始化使用拷贝初始化。</P><PRE><FONT color=#999933>  S* p = new S(4);          // new表达式</FONT></PRE>  
<P>new表达式使用直接初始化。<br><br>  
</P>  
</DIV></div></div>  
  
</center></BODY></HTML>  

⌨️ 快捷键说明

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