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

            健康,快樂,勇敢的寧帥!!

            努力、努力、再努力! 沒有什么能阻止我對知識的渴望。

             

            "C++Templates The Complete Guide"讀書筆記----Chapter 3

            Chapter 3 Class Templates
            1. A clas template is a class that is implemented with one or more type parameters left open
            #include?<vector>
            #include?
            <stdexcept>

            template?
            <typename?T>
            class?Stack?{
            ??
            private:
            ????std::vector
            <T>?elems;?????//?elements

            ??
            public:
            ????
            void?push(T?const&);??????//?push?element
            ????void?pop();???????????????//?pop?element
            ????T?top()?const;????????????//?return?top?element
            ????bool?empty()?const?{??????//?return?whether?the?stack?is?empty
            ????????return?elems.empty();
            ????}

            }
            ;

            template?
            <typename?T>
            void?Stack<T>::push?(T?const&?elem)
            {
            ????elems.push_back(elem);????
            //?append?copy?of?passed?elem
            }


            template
            <typename?T>
            void?Stack<T>::pop?()
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range("Stack<>::pop():?empty?stack");
            ????}

            ????elems.pop_back();?????????
            //?remove?last?element
            }


            template?
            <typename?T>
            T?Stack
            <T>::top?()?const
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range("Stack<>::top():?empty?stack");
            ????}

            ????
            return?elems.back();??????//?return?copy?of?last?element
            }
            2. To use a class template, you pass the open types as template arguments. The class template is the instantiated(and compiled) for these types
            3. For class templates, only those menber funcitons that are called are instantiated
            This , of course, saves time and space. It has the additional benefit that you can instantiate a class even for those types that cannot perform all the operations of all the menber functions, as long as these member functions are not called.
            Note that you have to put whitespace between the two closing? template brackets. If you don't do this, you are using operator>>,which results in a syntax error:
            Stack<Stack<int>>?intStackStack;?//ERROR:>>is?not?allowed
            4. You can specialize calss templates for certain types
            template<>
            class?Stack<std::string>?{
            ??
            private:
            ????std::deque
            <std::string>?elems;??//?elements

            ??
            public:
            ????
            void?push(std::string?const&);??//?push?element
            ????void?pop();?????????????????????//?pop?element
            ????std::string?top()?const;????????//?return?top?element
            ????bool?empty()?const?{????????????//?return?whether?the?stack?is?empty
            ????????return?elems.empty();
            ????}

            }
            ;

            void?Stack<std::string>::push?(std::string?const&?elem)
            {
            ????elems.push_back(elem);????
            //?append?copy?of?passed?elem
            }


            void?Stack<std::string>::pop?()
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range
            ????????????????(
            "Stack<std::string>::pop():?empty?stack");
            ????}

            ????elems.pop_back();?????????
            //?remove?last?element
            }


            std::
            string?Stack<std::string>::top?()?const
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range
            ????????????????(
            "Stack<std::string>::top():?empty?stack");
            ????}

            ????
            return?elems.back();??????//?return?copy?of?last?element
            }


            ps:也就是所謂的特定版本,模板元編程中的關(guān)鍵。
            5. You can partially specialize class templates for certain types
            If more than one partial specilalization matches equally well, the declaration is ambiguous
            6. You can define default values for class template parameters.These may refer to previous template parameters

            posted on 2006-11-26 16:15 ningfangli 閱讀(164) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            統(tǒng)計

            公告

            Dict.CN 在線詞典, 英語學(xué)習(xí), 在線翻譯

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            狠狠色丁香久久综合五月| 亚洲精品无码久久毛片| 亚洲中文久久精品无码ww16| 久久精品人人做人人爽电影 | 久久精品国产亚洲AV不卡| 久久免费观看视频| 日韩人妻无码一区二区三区久久| 人妻丰满AV无码久久不卡| 国产一区二区精品久久凹凸| 久久久久99这里有精品10 | 亚洲国产精品久久久久婷婷老年| 久久91这里精品国产2020| 中文字幕久久精品无码| 日韩亚洲欧美久久久www综合网| 久久婷婷五月综合国产尤物app| 麻豆精品久久久一区二区| 精品多毛少妇人妻AV免费久久| 精品精品国产自在久久高清| 囯产极品美女高潮无套久久久| 99久久国产综合精品五月天喷水| 东方aⅴ免费观看久久av | 久久久免费精品re6| 中文字幕久久亚洲一区| 久久99精品久久久久久野外| 99久久久国产精品免费无卡顿| 狠狠色噜噜色狠狠狠综合久久| 久久久国产精品| 国产午夜福利精品久久| 日本精品久久久中文字幕| 国产欧美久久一区二区| 久久亚洲私人国产精品| 18岁日韩内射颜射午夜久久成人| 亚洲?V乱码久久精品蜜桃 | 理论片午午伦夜理片久久| 国产日韩久久免费影院| 久久国产精品一区二区| 国产精品对白刺激久久久| 久久国产亚洲精品无码| 久久不见久久见免费视频7| 国内精品人妻无码久久久影院导航| 国产香蕉久久精品综合网|