• <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++技術 在這里寫下自己的學習心得 感悟 和大家討論 共同進步(歡迎批評!!?。?/p>

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              66 Posts :: 16 Stories :: 236 Comments :: 0 Trackbacks

            公告

            王一偉 湖南商學院畢業 電子信息工程專業

            常用鏈接

            留言簿(19)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 387834
            • 排名 - 64

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            The PIMPL idiom

            In C++ when anything in a header file changes, all code that includes the header (either directly or indirectly) must be recompiled. To minimalize this we use PIMPL idiom:

            // file x.h
            class X
            {
            public:
            // public members
            protected:
            // protected members
            private:
            // pointer to forward declared class
            class XImpl *pimpl_;  // opaque pointer
            };
            

            Questions: -- What should go into XImpl? Options are:

            • Put all private data but not functions into XImpl.: Not too bad, but there is better

            • Put all private members into XImpl.

            • Put all private and protected members into XImpl. Bad, protected members must be in X

            • Make XImpl entirely the class that X would have been, and write X as only the public interface made up entirely of simple forwarding functions (handle/body variant).

            -- Does XImpl require a pointer back to the X object?

            Caveats:

            • You can't hide virtual member functions in the Pimpl (here private inheritance differs from membership)

            • Functions in Pimpl may require a "back pointer" to the visible object (by convention that is called: self_.

            • Often the best compromise is to use Option 2, and in addition to put into XImpl only rhose non-private functions that need to be called by private ones.

            • 4th is better over 2nd not needed "back pointer", but X is useless for inheritance.

            PIPML has some drawbacks, like allocating/deallocating objects in the heap, which could be slow.

            What about this "optimalization"?

            // file: y.h
            class Y
            {
            //...
            static const size_t sizeofx = /* ... */;
            char x_[sizeofx];
            };
            // file: y.cpp
            #include "x.h"
            Y::Y()
            {
            assert( sizeofx >= sizeof(X) );
            new(&x_[0]) X;
            }
            Y::~Y()
            {
            (reinterpret_cast<X*>(&x_[0]))->~X();
            }
            

            Questions:

            • What is the Pimpl space overhead?

            • What is the performance overhead?

            Space overhead:

            #include <iostream>
            using namespace std;
            struct X {
            char c;
            struct XImpl *pimpl_;
            };
            struct XImpl { char c; };
            int main()
            {
            cout << sizeof(XImpl) << '\t' << sizeof(X) << endl;
            return 0;
            }
            // result: 1    8
            

            Runtime overhead:

            • allocation/deallocation cost: relativelly expensive

            • indirect access of private members (+ back pointer)

            • alignment problems: new guaranties, that object will align properly, char[] buffers doesn't!

            • X must not use the default assignmentoperator=()

            • If sizeof(XImpl) grows greater then sizeofx, we need to update the source.

            // file x.h
            class X
            {
            //...
            struct XImpl *pimpl_;
            };
            // file x.cpp
            #include "x.h"
            struct XImpl
            {
            // private stuff here ...
            static void *operator new(size_t)   { /*...*/ }
            static void *operator delete(void*) { /*...*/ }
            };
            X::X() : pimpl_( new XImpl ) { }
            X::~X() { delete pimpl_; pimpl_ = 0; }
            
            posted on 2007-07-16 14:51 @王一偉 閱讀(1307) 評論(0)  編輯 收藏 引用
            精品久久久久久无码人妻热| 亚洲国产精品狼友中文久久久| 草草久久久无码国产专区| 久久久人妻精品无码一区| 99久久99久久精品国产片果冻| 狠狠色丁香久久综合婷婷| 色婷婷综合久久久久中文字幕 | 亚洲AV伊人久久青青草原| 久久久久人妻一区二区三区 | 亚洲一区二区三区日本久久九| 婷婷久久综合九色综合九七| 日韩精品久久无码中文字幕| 久久国产精品99精品国产987| 亚洲欧美日韩精品久久亚洲区| 99久久国产综合精品麻豆| 亚洲午夜福利精品久久| 蜜桃麻豆www久久| 一本色道久久88精品综合| 精品99久久aaa一级毛片| 久久久久久亚洲Av无码精品专口| 久久av高潮av无码av喷吹| 久久99精品久久久久子伦| 亚洲精品无码久久久| 国产成人久久久精品二区三区 | 久久亚洲国产精品123区| 久久大香香蕉国产| 免费久久人人爽人人爽av| 久久精品无码av| 欧美久久综合性欧美| 亚洲级αV无码毛片久久精品| 久久亚洲AV无码西西人体| 青青青国产成人久久111网站| 欧洲成人午夜精品无码区久久| 免费精品久久久久久中文字幕 | 欧美va久久久噜噜噜久久| 久久伊人影视| 久久精品成人免费国产片小草| 国产亚洲精久久久久久无码| 亚洲va国产va天堂va久久| 欧美日韩久久中文字幕| 亚洲日本va午夜中文字幕久久|