• <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 點點滴滴 閱讀(211) 評論(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
            久久天天躁狠狠躁夜夜网站| 72种姿势欧美久久久久大黄蕉| 国产成人精品久久亚洲高清不卡 | 91精品国产综合久久四虎久久无码一级| 久久久久亚洲AV无码专区桃色| 久久久久国产精品人妻| 国产精品久久久久久搜索| 亚洲国产精品久久久久网站 | 国产成人精品三上悠亚久久| AA级片免费看视频久久| 深夜久久AAAAA级毛片免费看| 国产精品久久99| 中文字幕无码久久精品青草 | 久久99热这里只有精品66| 精品久久久久久无码人妻蜜桃| 97久久精品国产精品青草| 人人狠狠综合久久亚洲高清| 精品久久8x国产免费观看| 99久久国产宗和精品1上映| 国产精品免费看久久久香蕉| 久久久久高潮综合影院| 久久午夜福利电影| 久久久精品午夜免费不卡| 久久精品国产亚洲沈樵| 久久99国产精品一区二区| 亚洲乱码精品久久久久..| 无码专区久久综合久中文字幕| 久久久久综合中文字幕| 丁香久久婷婷国产午夜视频| 久久精品中文字幕久久| 久久久久久亚洲Av无码精品专口| 久久久久亚洲AV无码麻豆| 免费久久人人爽人人爽av| 亚洲AⅤ优女AV综合久久久| 久久99精品九九九久久婷婷| 嫩草影院久久国产精品| 97久久精品午夜一区二区| 久久国产一区二区| 2020最新久久久视精品爱| 国产精品美女久久久久av爽 | 天天爽天天狠久久久综合麻豆|