• <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++技術(shù) 在這里寫下自己的學(xué)習(xí)心得 感悟 和大家討論 共同進(jìn)步(歡迎批評(píng)!!!)

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              66 Posts :: 16 Stories :: 236 Comments :: 0 Trackbacks

            公告

            王一偉 湖南商學(xué)院畢業(yè) 電子信息工程專業(yè)

            常用鏈接

            留言簿(19)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 387834
            • 排名 - 64

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            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) 評(píng)論(0)  編輯 收藏 引用

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


            无码人妻精品一区二区三区久久久| 久久久亚洲欧洲日产国码是AV| 久久久人妻精品无码一区 | 99久久国产精品免费一区二区| 久久青青草原亚洲av无码app| 久久青青草原综合伊人| 久久久久国色AV免费观看| 久久久久人妻精品一区二区三区| 三上悠亚久久精品| 中文字幕精品久久久久人妻| 久久精品亚洲日本波多野结衣 | 久久天天日天天操综合伊人av| 亚洲国产精品18久久久久久| 久久久久久久人妻无码中文字幕爆 | 亚洲国产精品一区二区久久hs| 99国产精品久久| 日韩精品久久无码中文字幕 | 欧美精品国产综合久久| 久久综合九色综合久99| 精品免费久久久久久久| 奇米综合四色77777久久| 国产福利电影一区二区三区久久老子无码午夜伦不 | 欧洲精品久久久av无码电影| 久久精品国产99国产精品| 久久久久99精品成人片欧美| 欧美日韩中文字幕久久久不卡| 国产一区二区三精品久久久无广告| 久久久亚洲欧洲日产国码aⅴ| 久久久久无码中| 国产精品九九久久免费视频 | av国内精品久久久久影院| 国产美女亚洲精品久久久综合| 精品水蜜桃久久久久久久| 亚洲一区中文字幕久久| 久久99中文字幕久久| 99久久精品免费观看国产| 久久99精品久久久久久久不卡| 久久99精品久久久久子伦| 国产韩国精品一区二区三区久久| 久久99精品久久久久久hb无码| 一本色综合网久久|