• <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>
            隨筆-3  評(píng)論-0  文章-0  trackbacks-0
              2010年1月20日
                 typedef的用途大家都是很熟悉的了,遇到很長(zhǎng)的標(biāo)識(shí)又不便閱讀和表示的時(shí)候,就可以
            重新定義更有閱讀性和表現(xiàn)力的標(biāo)識(shí)。那么,typedef是不是就像險(xiǎn)惡的宏替換呢?開(kāi)始我也
            是這么認(rèn)為的,直到看到一個(gè)例子。
            1 typedef const* constPointer
            2 int constPointer pSomeThing;
                   現(xiàn)在的問(wèn)題是,指針pSomeThing是一個(gè)常指針呢,還是一個(gè)指向一個(gè)常量的可變指針?
            答案可能會(huì)出乎一般人的意料。因?yàn)槿绻皇前凑瘴谋咎鎿Q的語(yǔ)義,pSomeThing應(yīng)該是一個(gè)
            指向一個(gè)int常量的指針。可是實(shí)際上它是一個(gè)常指針,指向的int量的值是可變的。
                   這就是typedef和宏不同的地方。它在定義新的標(biāo)識(shí)的時(shí)候,并不深入標(biāo)識(shí)的內(nèi)部判斷,
            而是整個(gè)標(biāo)識(shí)作為一個(gè)整體表達(dá)語(yǔ)義,因此,constPointer表達(dá)的是“常指針”的語(yǔ)義,const
            限定符作用于指針之上。就是這么簡(jiǎn)單。

            posted @ 2010-01-20 09:44 Leix 閱讀(335) | 評(píng)論 (0)編輯 收藏
              2010年1月19日
            模板函數(shù)不支持template template parameter。例如:
            1 template< template<class T>  class Foo>
            2 void bar( Foo<T> test )
            3 {
            4     //do something 
            5 }
            注意,這是編譯不通過(guò)的,編譯器會(huì)提示說(shuō)參數(shù)T無(wú)效。但是有時(shí)候咱們必須依賴T,怎么辦呢?
            其實(shí)只要引入一個(gè)輔助的參數(shù)就可以啦:
            1 template< template<class> class Foo, class T>
            2 void bar( Foo<T> test )
            3 {
            4     //do something 
            5 }


            posted @ 2010-01-19 19:41 Leix 閱讀(197) | 評(píng)論 (0)編輯 收藏
              2010年1月18日
                  學(xué)習(xí)C++有一段時(shí)間了,最近幾個(gè)月才有所心得,希望在這里和大家共同分享。
                  最近利用一點(diǎn)時(shí)間寫(xiě)了一個(gè)matrix鍛煉一下自己,因?yàn)閷W(xué)習(xí)的時(shí)候都會(huì)用到,所以自己就隨便寫(xiě)寫(xiě)。中間學(xué)到不少。
            比如,最奇怪的是,在我這個(gè)LMatrix<T>模板類里,直接使用setioflags會(huì)提示說(shuō)“setioflags不是模板類,不支持模板參數(shù)”。
            后來(lái)在書(shū)上看到,在頭文件加上
            1 using std::setioflags
            就完全沒(méi)有問(wèn)題了。真是不得其解啊!
                   另外,intel C++ Compiler 11 在編譯的時(shí)候,沒(méi)有任何問(wèn)題,但g++ 4.4.1(在ubuntu 9.10 下)卻有這么個(gè)提示:
            1 template<typename T>
            2 const LMatrix<T>::Matrix2p LMatrix<T>::lu_resolve() const
            說(shuō)在”<"之前要初始化LMatrix<T>,而Matrix2p只是LMatrix<T>里的一個(gè)嵌套類。我測(cè)試了這個(gè)函數(shù),完全正常,只是g++ 不能編譯通過(guò)。期待高人解決之。
                   由于intel還不支持右值引用,所以代碼里的時(shí)髦函數(shù)子就只好處理掉了。
                   看了《efficient c++》,本來(lái)想把它的固定大小的內(nèi)存池拿來(lái)直接用,結(jié)果發(fā)現(xiàn)最后釋放內(nèi)存池的時(shí)候,崩潰了。代碼如下:

             1 template<typename T>
             2 class LMatrix
             3 {
             4 public:
             5 //
             6 // other functions
             7 
             8 ///////////overload operator new and delete to use the memorypool////////////////
             9 inline void* operator new(size_t size)
            10 
            11      return memPool->alloc(size);
            12 }
            13 
            14 inline void operator delete(void* element, size_t size)
            15 
            16      memPool->free(element); 
            17 }
            18 
            19 static void newMemoryPool()
            20 
            21     memPool = new ByteMemoryPool; 
            22 }
            23 
            24 static void deleteMemoryPool()
            25 
            26    delete memPool;
            27 }
            28 ////////////////////////////////////
            29 
            30 private:
            31    static ByteMemoryPool* memPool;
            32 }

                    內(nèi)存池LMemoryPool.hpp的代碼原樣引自《efficient C++》:
             1 #ifndef L_MEMORYPOOL_H_
             2 #define L_MEMORYPOOL_H_
             3 #include "LMatrix.hpp"
             4 
             5 template<typename T>
             6 class LMemoryPool
             7 {
             8 public:
             9     LMemoryPool(size_t size = EXPANSION_SIZE);
            10     ~LMemoryPool();
            11     inline void* alloc(size_t size);
            12     inline void free(void* element);
            13 private:
            14     LMemoryPool<T>* next;
            15     static const size_t EXPANSION_SIZE = 32;
            16     void expandList(size_t listLength = EXPANSION_SIZE);
            17 };
            18 
            19 template<typename T>
            20 LMemoryPool<T>::LMemoryPool(size_t size)
            21 {
            22     expandList(size);
            23 }
            24 
            25 template<typename T>
            26 LMemoryPool<T>::~LMemoryPool()
            27 {
            28     LMemoryPool<T>* pNext = next;
            29     for(pNext = next; pNext != NULL; pNext = next)
            30     {
            31         next = next->next;
            32         delete[] pNext;
            33     }
            34 }
            35 
            36 template<typename T>
            37 void* LMemoryPool<T>::alloc(size_t size)
            38 {
            39     if(next != 0)
            40         expandList();
            41     LMemoryPool<T>* head = next;
            42     next = head->next;
            43     return head;
            44 }
            45 
            46 template<typename T>
            47 void LMemoryPool<T>::free(void* element)
            48 {
            49     LMemoryPool<T>* head = static_cast< LMemoryPool<T>* >(element);
            50     head->next = next;
            51     next = head;
            52 }
            53 
            54 template<typename T>
            55 void LMemoryPool<T>::expandList(size_t listLength)
            56 {
            57     size_t size = (sizeof(T) > sizeof(LMemoryPool<T>*))
            58                 ? sizeof(T) : sizeof(LMemoryPool<T>*);
            59     LMemoryPool<T>* itr = reinterpret_cast< LMemoryPool<T>* >(new char[size]);
            60     
            61     next = itr;
            62     
            63     for(size_t i=0; i<listLength; ++i)
            64     {
            65         itr->next = reinterpret_cast< LMemoryPool<T>* >(new char[size]);
            66         itr = itr->next;
            67     }
            68     itr->next = NULL;
            69     
            70 }
            71 #endif /*L_MEMORYPOOL_H_ */

            測(cè)試的testMatrix.cpp用例如下:
             1 #include "LMatrix.hpp"
             2 LMemoryPool< LMatrix<int> >* LMatrix<int>::memPool = 0;
             3 int main( )
             4 {
             5     const int size = 4;
             6     LMatrix<int>* array[size];
             7     LMatrix<int>::newMemoryPool();
             8     for(int j=0; j<size / 2++j)
             9     {
            10         for(int i=0; i<size; ++i)
            11             array[i] = new LMatrix<int>(88);
            12        
            13         for(int k=0; k<size; ++k)   
            14             delete array[k];
            15     }
            16 
            17     LMatrix<int>::deleteMemoryPool();
            18 
            19     return 0;
            20 }
            21 
                   一切正常直到上面測(cè)試程序的第17行,追蹤到內(nèi)存池LMemoryPool.hpp的析構(gòu)函數(shù)(第32行),發(fā)現(xiàn)它竟然被多次執(zhí)行了。這不是很奇怪嗎?
            類的靜態(tài)成員是全局共享的,這里只有一次調(diào)用,為什么會(huì)造成多次析構(gòu)呢?期待高人。

            posted @ 2010-01-18 20:29 Leix 閱讀(206) | 評(píng)論 (0)編輯 收藏
            僅列出標(biāo)題  
            欧美成人免费观看久久| 伊人色综合久久天天人手人婷 | 日本欧美国产精品第一页久久| 久久人人爽人人爽AV片| 一本久道久久综合狠狠爱| 久久―日本道色综合久久| 中文字幕精品久久| 麻豆精品久久久一区二区| 亚洲国产精品一区二区三区久久| 蜜臀av性久久久久蜜臀aⅴ| 日韩久久无码免费毛片软件| 国产成年无码久久久久毛片| 中文字幕无码久久久| 欧美亚洲另类久久综合| 中文字幕人妻色偷偷久久| 久久久精品久久久久特色影视| 久久国产高潮流白浆免费观看| 狠狠色丁香久久婷婷综合图片| 99久久99久久精品国产片| 久久久久久久久无码精品亚洲日韩 | 久久国产亚洲精品麻豆| 久久久精品国产免大香伊| 久久久久亚洲AV无码去区首| 久久噜噜电影你懂的| 久久久女人与动物群交毛片| 久久99精品国产麻豆宅宅| 久久无码一区二区三区少妇| 国产精品一久久香蕉产线看| 亚洲国产精品无码久久| 久久久久久精品免费免费自慰| 亚洲午夜久久久| 亚洲伊人久久成综合人影院 | 国内精品久久久久影院一蜜桃| 国产成人精品综合久久久| 亚洲国产精品成人久久蜜臀| 伊人热人久久中文字幕| 久久精品这里热有精品| 99久久精品免费| 亚洲综合久久综合激情久久| 久久国产精品99久久久久久老狼| 97久久精品无码一区二区天美|