隨筆-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 閱讀(353) 評論(0)  編輯  收藏 所屬分類: C/C++

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


          網站導航:
           
          主站蜘蛛池模板: 凤庆县| 闽清县| 海晏县| 莱州市| 宁海县| 措勤县| 驻马店市| 新绛县| 鹰潭市| 门源| 五河县| 大关县| 天等县| 米易县| 临城县| 黑山县| 页游| 兴和县| 石家庄市| 横峰县| 铜鼓县| 池州市| 涡阳县| 鄂温| 突泉县| 华坪县| 星子县| 隆尧县| 安阳市| 那曲县| 万年县| 安多县| 黄山市| 柳江县| 鞍山市| 平潭县| 抚州市| 万载县| 大足县| 尼勒克县| 黎城县|