• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 311, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            (地基工)friend functions

            Posted on 2011-03-02 15:57 點點滴滴 閱讀(215) 評論(0)  編輯 收藏 引用 所屬分類: 02 編程語言

            設有一個函數(shù) int f(const A &a) 含有一個引數(shù) a, 其為 class A 的一物件, 又 函數(shù) f 並不是 class A 的一個 memeber function, 若於 函數(shù) f 的定義中, 欲能自由引用 class A 的 private elements 如同 class A 的 member function 一般, 則需要在 class A 中 宣告為一 friend function, (就好像得到 class A 的允許, 可以使用其 公用 與 私用 資料 ), 其 宣告方式如下:

            class A
            {
            public:
                A(
            int r=0){x=r;}
                friend 
            int f(const A &a);
            private:
                
            int x;
            }
            int f(const A &a)
            {
                
            return a.x*a.x;
            }
            說明:
            1. 函數(shù)int f(const A &a) 的設計, 可以在 class A 的定義之內(nèi),或在 class A 的定義之外, 如上述情況。
            2. 函數(shù)int f(const A &a) 並非是 class A 的一個 member function, 因此, 於上述情況 函數(shù)int f(const A &a) 的定義並不加上 A:: 的標示。
            3. 函數(shù)int f(const A &a) 於 class A 的宣告地方, 不限定在 public 或 private 區(qū)。

            設有一個函數(shù) int h(const A &a, const B &b) 含有兩個引數(shù) a 與 b, 其分別為 class A 與 class B 的物件, 又函數(shù) h 既不是 class A 的 memeber function, 也不是 class B 的 memeber function。 若 函數(shù) h 欲能自由引用 class A 與 class B 的 private elements, 則需要在 class A 與 class B 中 宣告為一 friend function, 其 宣告方式如下例:

            例 4: a friend function of 2 different classes。

            #include <iostream.h>
            class B;
            class A
            {
            public:
                A(
            int r=0){x=r;}
                
            ~A(){}
                friend 
            int h(const A &a, const B &b);
            private:
                
            int x;
            };
            class B
            {
            public:
                B(
            int r=0){y=r;}
                
            ~B(){}
                friend 
            int h(const A &a, const B &b);
            private:
                
            int y;
            };
            int h(const A &a, const B &b)
            {
                
            return a.x*b.y;
            }
            main()
            {
                A a(
            10);
                B b(
            20);
                cout
            << "h() = "<< h(a, b)<< endl;
            }
            說明:
            1. 函數(shù)int h(const A &a, const B &b) 同時為 class A 與 class B 的 friend function。
            2. 該程式的執(zhí)行結(jié)果為 h() = 200
            久久免费视频观看| 五月丁香综合激情六月久久| 久久久久久综合一区中文字幕 | 无码人妻精品一区二区三区久久| 免费久久人人爽人人爽av| 久久久一本精品99久久精品88| 婷婷伊人久久大香线蕉AV| 97精品伊人久久大香线蕉app| 国内精品免费久久影院| 丁香色欲久久久久久综合网| 国产精品久久毛片完整版| 久久久久噜噜噜亚洲熟女综合| 久久久WWW成人免费毛片| 久久久一本精品99久久精品88| 51久久夜色精品国产| 色综合久久久久无码专区| 久久久久久A亚洲欧洲AV冫| 精品久久久久久中文字幕人妻最新| 99久久国产主播综合精品| 久久久亚洲欧洲日产国码二区 | .精品久久久麻豆国产精品| 久久久精品波多野结衣| 久久精品国内一区二区三区 | 久久久久国产精品人妻| 四虎国产精品免费久久久| 无码AV波多野结衣久久| 无码人妻久久一区二区三区蜜桃| 中文精品久久久久国产网址| 久久国产精品99精品国产| 伊人久久大香线蕉综合Av| 久久99热这里只频精品6| 精品久久久久久国产三级| 九九久久99综合一区二区| 久久男人Av资源网站无码软件| 伊人久久大香线蕉av一区| 亚洲国产天堂久久综合| 久久综合偷偷噜噜噜色| 久久精品国产亚洲AV影院| 久久久久亚洲av综合波多野结衣| 热久久视久久精品18| 国产精品久久久久久久久久影院|