隨筆-348  評論-598  文章-0  trackbacks-0
          void ?fu(X *& ?x)
          {
          ????x
          = new ?X();
          ????cout
          <<& x << endl;
          }

          int ?main()
          {

          ????X
          * ?c = NULL;
          ????
          ????fu(c);
          ????cout
          <<& c << endl;
          ????c
          -> display();


          ????
          return ? 0 ;
          }

          參數帶引用的話只可以改變參數的值的,也和雙指針一樣,但貌似傳遞引用更加保險,不會因為疏忽而改變了指針的指針的值
          void?fu(X**?x)
          {
          ????
          *x=new?X();
          ????cout
          <<*x<<endl;
          }

          int?main()
          {

          ????X
          *?c=NULL;
          ????
          ????fu(
          &c);
          ????cout
          <<c<<endl;
          ????c
          ->display();


          ????
          return?0;
          }

          Pass-by-Reference versus Pass-by-Value
          Pass-by-reference is required when you want to modify the parameter and see those changes reflected
          in the variable argument to the function or method However, you should not limit your use of pass-byreference
          to only those cases. Pass-by-reference avoids copying the argument to the function, providing
          two additional benefits in some cases:
          1. Efficiency: large objects and structs could take a long time to copy. Pass-by-reference passes
          only a pointer to the object or struct into the function.
          2. Correctness: not all objects allow pass-by-value. Even those that do allow it might not support
          deep copying correctly. As you learned in Chapter 9, objects with dynamically allocated memory
          must provide a custom copy constructor in order to support deep copying.
          If you want to leverage these benefits, but do not want to allow the original objects to be modified, you
          can mark the parameters const. This topic is covered in detail later in this chapter.
          These benefits to pass-by-reference imply that you should use pass-by-value only for simple built-in
          types like int and double for which you don’t need to modify the arguments. Use pass-by-reference in
          all other cases.

          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2007-03-17 11:20 TiGERTiAN 閱讀(348) 評論(0)  編輯  收藏 所屬分類: C/C++

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 依兰县| 南汇区| 吉水县| 梁河县| 夏河县| 三明市| 贞丰县| 来安县| 柞水县| 固原市| 天津市| 扬州市| 恩施市| 宜章县| 民权县| 瑞金市| 逊克县| 海晏县| 什邡市| 合肥市| 南投县| 荥经县| 将乐县| 余干县| 孟连| 合肥市| 乌鲁木齐市| 广河县| 酒泉市| 霍州市| 宁明县| 财经| 龙口市| 东兴市| 大理市| 黄龙县| 尼玛县| 佛坪县| 左云县| 通榆县| 新巴尔虎右旗|