• <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>

            CG@CPPBLOG

            /*=========================================*/
            隨筆 - 76, 文章 - 39, 評論 - 137, 引用 - 0
            數(shù)據(jù)加載中……

            《C++設(shè)計新思維》讀書筆記(三)


             
            1.5.1 運(yùn)用Template Template參數(shù)實(shí)作Policy Classes

            如前例,庫代碼host class如果已知policy class,那么可以這樣描述:
            1 
            2 //Library code
            3 template < template < class Created > class CreationPolicy >
            4 class WidgetManager : public CreationPolicy<Widget>{};


            因?yàn)镃reated只是形式引數(shù)(formal argument),不可使用,可以省略如下:
            1 
            2 template < template < class > class CreationPolicy >
            3 class WidgetManager : public CreateionPolicy<Widget>{};


            客戶代碼為:

            1 typedef WidgetManager<OpNewCreator> MyWidgetMgr;


            當(dāng)WidgetManager希望在內(nèi)部以相同的生成策略產(chǎn)生一個內(nèi)部對象,那么這種template template形式不可或缺。

            Policy的確能夠帶給WidgetManager非常大的彈性。第一,可以外部變更policies。第二,可以自定義policies。

            WidgetManager的作者可以定義一些常用的policies,以“template 缺省引數(shù)”的方式提供:
            1 
            2 template < template < class > class CreationPolicy = OpNewCreator >
            3 class WidgetManager .


            1.5.2 運(yùn)用Template 成員函數(shù)實(shí)作Policy Classes

            我們可以把先前的Creator policy定義為一個非模板類,內(nèi)部提供一個模板成員函數(shù)如下:
             1 
             2 struct OpNewCreator
             3 {
             4      template < class T >
             5      static T* Create()
             6      {
             7             return new T;
             8      }
             9 }
            10 

            這種方式對舊式編譯期有較佳兼容性。但難以討論、定義、實(shí)作和運(yùn)用。

            1.6 更豐富的Policies

            在前例的Creator policy中PrototypeCreateor除了提供Create()成員函數(shù)外,還提供了GetProtoType和SetProtoType兩個函數(shù),由于WidgetManager繼承了policy class,所以WidgetManager具有此兩個接口,雖然它自己并沒有用。

            客戶代碼可以如此:
            1 
            2 typedef WidgetManager<PrototypeCreator> MyWidgetManager;
            3 /////////
            4 widget* pPrototype = ;
            5 MyWidgetManager mgr;
            6 mgr.SetPrototype(pPrototype);


            使用者如果需要擴(kuò)充policies,可以在不影響host class原本功能的前提下,增加接口。“哪個policy被使用”由使用者決定而非程序庫自身。policies給與使用者一種能力,在型別安全的前提下擴(kuò)增host class的功能。

             

            posted on 2007-12-17 21:44 cuigang 閱讀(395) 評論(0)  編輯 收藏 引用 所屬分類: 《C++設(shè)計新思維》讀書筆記

            久久精品国产亚洲AV忘忧草18| 伊人久久久AV老熟妇色| 久久久久久久综合日本| 日本五月天婷久久网站| 久久AV高清无码| 精品国产91久久久久久久a| 久久狠狠爱亚洲综合影院| 久久最近最新中文字幕大全| 怡红院日本一道日本久久 | 人人狠狠综合久久亚洲高清| 老男人久久青草av高清| 亚洲国产成人久久精品影视| 久久99精品国产麻豆宅宅| 国产高潮久久免费观看| 五月丁香综合激情六月久久 | 久久精品日日躁夜夜躁欧美| 亚洲狠狠久久综合一区77777| 亚洲va国产va天堂va久久| 久久青青草原精品国产软件| 国产日产久久高清欧美一区| 一级A毛片免费观看久久精品| 国产精品99久久久久久www| 精品久久久久香蕉网| 精品国产青草久久久久福利| 中文字幕精品无码久久久久久3D日动漫 | 国产精品伊人久久伊人电影| 国产人久久人人人人爽 | 久久国产香蕉一区精品| 国产99精品久久| 东京热TOKYO综合久久精品| 午夜天堂精品久久久久| 精品一二三区久久aaa片| 97精品国产97久久久久久免费| 亚洲色欲久久久久综合网| 日韩久久久久中文字幕人妻| 久久午夜综合久久| 久久久黄片| 伊人色综合久久天天人守人婷| 亚洲国产成人精品女人久久久| 色欲综合久久躁天天躁| 免费一级做a爰片久久毛片潮|