• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            (地基工)friend functions

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

            設有一個函數 int f(const A &a) 含有一個引數 a, 其為 class A 的一物件, 又 函數 f 並不是 class A 的一個 memeber function, 若於 函數 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. 函數int f(const A &a) 的設計, 可以在 class A 的定義之內,或在 class A 的定義之外, 如上述情況。
            2. 函數int f(const A &a) 並非是 class A 的一個 member function, 因此, 於上述情況 函數int f(const A &a) 的定義並不加上 A:: 的標示。
            3. 函數int f(const A &a) 於 class A 的宣告地方, 不限定在 public 或 private 區。

            設有一個函數 int h(const A &a, const B &b) 含有兩個引數 a 與 b, 其分別為 class A 與 class B 的物件, 又函數 h 既不是 class A 的 memeber function, 也不是 class B 的 memeber function。 若 函數 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. 函數int h(const A &a, const B &b) 同時為 class A 與 class B 的 friend function。
            2. 該程式的執行結果為 h() = 200
            国产精品无码久久四虎| 国产99久久久国产精品小说| 午夜欧美精品久久久久久久| 99精品久久久久久久婷婷 | 久久久久久久国产免费看| 久久国产精品免费一区二区三区| 四虎国产精品免费久久| 无码人妻久久一区二区三区免费| 国产精品一区二区久久| 人人狠狠综合久久亚洲| 国产精品禁18久久久夂久| 久久久久亚洲精品男人的天堂| 伊人久久大香线蕉亚洲| 中文精品久久久久国产网址| 亚洲精品国精品久久99热| 韩国无遮挡三级久久| 久久精品国产亚洲AV久 | 久久国产成人午夜aⅴ影院| 狠狠色婷婷久久综合频道日韩 | 久久精品www人人爽人人| 日本精品久久久久久久久免费| 久久久av波多野一区二区| 久久精品无码一区二区三区免费| 国产午夜精品久久久久免费视| 女同久久| 久久这里有精品视频| 久久久九九有精品国产| 精品熟女少妇a∨免费久久| 久久精品国产AV一区二区三区| 国产—久久香蕉国产线看观看| 久久午夜无码鲁丝片| 久久精品国产亚洲AV蜜臀色欲| 亚洲国产精品无码久久久久久曰| 成人亚洲欧美久久久久| 亚洲国产精久久久久久久| 久久精品天天中文字幕人妻| 色综合久久综合中文综合网| 精品一二三区久久aaa片| 无码任你躁久久久久久| 亚洲人成网站999久久久综合| 久久精品免费网站网|