• <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í)心得 感悟 和大家討論 共同進步(歡迎批評!!!)

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

            公告

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

            常用鏈接

            留言簿(19)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 387044
            • 排名 - 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 @王一偉 閱讀(1301) 評論(0)  編輯 收藏 引用

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


            久久精品卫校国产小美女| 国产综合免费精品久久久| 精品多毛少妇人妻AV免费久久| 色欲久久久天天天综合网| 亚洲午夜福利精品久久| 久久有码中文字幕| 日韩精品无码久久一区二区三| 狠狠色丁香久久婷婷综合蜜芽五月| 青青草原综合久久大伊人导航| 欧美麻豆久久久久久中文| 欧美精品九九99久久在观看| 四虎国产精品成人免费久久| 久久99久国产麻精品66| 精品久久久久中文字幕日本| 久久免费美女视频| 久久99精品久久久久久9蜜桃 | 伊人久久综合精品无码AV专区| 日本五月天婷久久网站| 久久久久人妻精品一区二区三区| 国产精品99精品久久免费| 久久97精品久久久久久久不卡| 国产 亚洲 欧美 另类 久久| 天天综合久久一二三区| 久久久免费精品re6| 99久久精品免费观看国产| 中文字幕精品无码久久久久久3D日动漫 | 伊人久久大香线蕉AV色婷婷色| 国产精品一区二区久久国产| 久久99久久无码毛片一区二区| 欧美国产成人久久精品| 久久99热国产这有精品| 亚洲欧洲精品成人久久曰影片| 亚洲精品乱码久久久久久自慰| 国产成人久久精品麻豆一区| 亚洲精品国精品久久99热一| 青青青伊人色综合久久| 人妻无码精品久久亚瑟影视 | 久久久免费观成人影院| 国内精品久久久久久99| 亚洲午夜精品久久久久久浪潮| 久久精品www|