• <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)  編輯 收藏 引用
            国产亚州精品女人久久久久久| 97精品依人久久久大香线蕉97 | 色婷婷综合久久久久中文| 久久精品国产色蜜蜜麻豆| 国产精品丝袜久久久久久不卡| 99久久成人国产精品免费| 97久久精品国产精品青草| 99久久精品毛片免费播放| 久久久国产精品福利免费| 国产亚洲成人久久| 久久人搡人人玩人妻精品首页| 欧美激情精品久久久久久久九九九| 欧美激情精品久久久久久| 色天使久久综合网天天| 久久综合给合久久国产免费 | 麻豆精品久久久久久久99蜜桃| 欧美与黑人午夜性猛交久久久| 国产精品久久久天天影视香蕉 | 久久久91精品国产一区二区三区| 欧美777精品久久久久网| 久久无码一区二区三区少妇 | 国产免费久久精品99久久| 久久WWW免费人成—看片| 久久久久久久波多野结衣高潮| 亚洲午夜久久久久久久久电影网| 国产精品欧美久久久天天影视| 久久久久无码精品国产app| 久久笫一福利免费导航 | 久久久久亚洲国产| 久久美女网站免费| 伊人精品久久久久7777| 久久91精品国产91久久小草| 日本久久久久久久久久| 久久精品国产亚洲av水果派| 欧美性猛交xxxx免费看久久久| 人妻无码αv中文字幕久久| 久久久久无码国产精品不卡| 青草国产精品久久久久久| a级毛片无码兔费真人久久| 久久综合综合久久综合| 亚洲国产天堂久久久久久|