• <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国产精品久久久| 久久强奷乱码老熟女| 狠狠色狠狠色综合久久| 久久久久久亚洲AV无码专区| 亚洲嫩草影院久久精品| 亚洲欧洲精品成人久久曰影片| 亚洲精品无码久久久久| 中文精品久久久久国产网址| 日韩亚洲国产综合久久久| 国产精品久久久久…| 久久亚洲精品国产精品婷婷| 99国产欧美久久久精品蜜芽| 久久久久久久久久久| 久久93精品国产91久久综合| 亚洲AV无码久久精品蜜桃| 国内精品久久久久久久亚洲 | 99久久这里只精品国产免费| 久久精品中文闷骚内射| 色妞色综合久久夜夜| 久久综合狠狠综合久久激情 | 亚洲色欲久久久综合网| 久久亚洲中文字幕精品一区| 久久亚洲国产午夜精品理论片| 久久久久亚洲av成人网人人软件 | 久久久久国产精品熟女影院| 久久无码一区二区三区少妇 | 亚洲国产小视频精品久久久三级 | 久久久久青草线蕉综合超碰| 久久久久亚洲AV无码去区首| 91精品国产91久久久久久| 国产精品岛国久久久久| av无码久久久久不卡免费网站| 东方aⅴ免费观看久久av| 久久综合一区二区无码| 亚洲国产成人久久综合区| 久久国产精品免费一区| 女同久久| 精品伊人久久大线蕉色首页| 亚洲精品无码久久久久sm| 久久久久99精品成人片试看 | 精品欧美一区二区三区久久久 |