• <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>
            asm, c, c++ are my all
            -- Core In Computer
            posts - 139,  comments - 123,  trackbacks - 0

            /********************************************\
            |????歡迎轉(zhuǎn)載, 但請(qǐng)保留作者姓名和原文鏈接, 祝您進(jìn)步并共勉!???? |
            \********************************************/


            C++對(duì)象模型(6) -? Program Transformation Semantics

            作者: Jerry Cat
            時(shí)間: 2006/05/11
            鏈接:?
            http://www.shnenglu.com/jerysun0818/archive/2006/05/11/6912.html

            2.3 Program Transformation Semantics
            =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
            1). Explicit Initialization:

            Given the definition
            X x0;
            the following three definitions each explicitly initialize its class object with x0:

            void foo_bar() {
            ?? X x1( x0 );
            ?? X x2 = x0;
            ?? X x3 = x( x0 );
            ?? // ...
            }
            The required program transformation is two-fold:

            Each definition is rewritten with the initialization stripped out.
            An invocation of the class copy constructor is inserted.
            For example, foo_bar() might look as follows after this straightforward, two-fold transformation:

            // Possible program transformation Pseudo C++ Code
            void foo_bar() {
            ?? X x1;
            ?? X x2;
            ?? X x3;

            ?? // compiler inserted invocations of copy constructor for X
            ?? x1.X::X( x0 );
            ?? x2.X::X( x0 );
            ?? x3.X::X( x0 );
            ?? // ...
            }
            where the call

            x1.X::X( x0 );
            represents a call of the copy constructor

            X::X( const X& xx );

            2). Argument Initialization盡量不用傳值法, 要穿指針或引用. 傳值法開銷大效率低,
            ??? 更要命的是涉及到深淺拷貝以及, 局部變量和臨時(shí)對(duì)象的銷毀問題.

            3). Return Value Initialization(雙重變形, Bjarne Stroutstrup的trick):
            (按: 返回值(不是引用或指針,返回的是value), 其實(shí)是讓一外部對(duì)象的引用做一個(gè)"悄然追加"
            ???? 的參數(shù)(編譯器偷著干的, 你是看不見的:), 然后是空返回, 你的返回值呢? 諾, 就是那
            ???? 以"外追"方式進(jìn)入函數(shù)內(nèi)部參與處理的引用呵^_^ )

            Given the following definition of bar():
            X bar()
            {
            ?? X xx;
            ?? // process xx ...
            ?? return xx;
            }
            you may ask how might bar()'s return value be copy constructed from its local object xx?
            Stroustrup's solution in cfront is a two-fold transformation:

            Add an additional argument of type reference to the class object. This argument will hold the
            copy constructed "return value."

            Insert an invocation of the copy constructor prior to the return statement to initialize the
            added argument with the value of the object being returned.

            What about the actual return value, then? A final transformation rewrites the function to have
            it not return a value. The transformation of bar(), following this algorithm, looks like this:

            // function transformation to reflect application of copy constructor Pseudo C++ Code
            void bar( X& __result )
            {
            ?? X xx;

            ?? // compiler generated invocation of default constructor
            ?? xx.X::X();
            ?? // ... process xx

            ?? // compiler generated invocation of copy constructor
            ?? __result.X::X( xx );

            ?? return;
            }
            Given this transformation of bar(), the compiler is now required to transform each invocation
            of bar() to reflect its new definition. For example,

            X xx = bar();
            is transformed into the following two statements:

            // note: no default constructor applied
            X xx;
            bar( xx );
            while an invocation such as

            bar().memfunc();
            might be transformed into

            // compiler generated temporary
            X __temp0;
            ( bar( __temp0 ), __temp0 ).memfunc();
            Similarly, if the program were to declare a pointer to a function, such as

            X ( *pf )();
            pf = bar;
            that declaration, too, would need to be transformed:

            void ( *pf )( X& );
            pf = bar;

            4). Optimization at the Compiler Level:
            In a function such as bar(), where all return statements return the same named value, it is
            possible for the compiler itself to optimize the function by substituting the result argument
            for the named return value. For example, given the original definition of bar():

            X bar()
            {
            ?? X xx;
            ?? // ... process xx
            ?? return xx;
            }
            __result is substituted for xx by the compiler:

            void
            bar( X &__result )
            {
            ?? // default constructor invocation Pseudo C++ Code
            ?? __result.X::X();
            ?? // ... process in __result directly

            ?? return;
            }
            This compiler optimization, sometimes referred to as the Named Return Value (NRV) optimization.

            Although the following three initializations are semantically equivalent:

            X xx0( 1024 );
            X xx1 = X( 1024 );
            X xx2 = ( X ) 1024;
            in the second and third instances, the syntax explicitly provides for a two-step initialization:
            Initialize a temporary object with 1024.

            Copy construct the explicit object with the temporary object.

            That is, whereas xx0 is initialized by a single constructor invocation

            // Pseudo C++ Code
            xx0.X::X( 1024 );
            a strict implementation of either xx1 or xx2 results in two constructor invocations, a temporary
            object, and a call to the destructor of class X on that temporary object:

            // Pseudo C++ Code
            X __temp0;
            __temp0.X::X( 1024 );
            xx1.X::X( __temp0 );
            __temp0.X::~X();

            5). The Copy Constructor: To Have or To Have Not?
            ??? =============================================
            Given the following straightforward 3D point class:

            class Point3d {
            public:
            ?? Point3d( float x, float y, float z );
            ?? // ...
            private:
            ?? float _x, _y, _z;
            };
            should the class designer provide an explicit copy constructor?

            The default copy constructor is considered trivial. There are no member or base class objects
            with a copy constructor that need to be invoked. Nor is there a virtual base class or virtual
            function associated with the class. So, by default, a memberwise initialization of one Point3d
            class object with another results in a bitwise copy. This is efficient. But is it safe?

            The answer is yes. The three coordinate members are stored by value. Bitwise copy results in
            neither a memory leak nor address aliasing. Thus it is both safe and efficient.

            So, how would you answer the question, should the class designer provide an explicit copy
            constructor? The obvious answer, of course, is no. There is no reason to provide an instance
            of the copy constructor, as the compiler automatically does the best job for you. The more subtle
            answer is to ask whether you envision the class's requiring a good deal of memberwise
            initialization, in particular, returning objects by value? If the answer is yes, then it makes
            excellent sense to provide an explicit inline instance of the copy constructor that is, provided
            your compiler provides the NRV optimization(虛擬語氣).

            For example, the Point3d class supports the following set of functions:

            Point3d operator+( const Point3d&, const Point3d& );
            Point3d operator-( const Point3d&, const Point3d& );
            Point3d operator*( const Point3d&, int );
            etc.
            all of which fit nicely into the NRV template
            {
            ?? Point3d result;
            ?? // compute result
            ?? return result
            }
            The simplest method of implementing the copy constructor is as follows:

            Point3d::Point3d( const Point3d &rhs )
            {
            ?? _x = rhs._x;
            ?? _y = rhs._y;
            ?? _z = rhs._z;
            };
            This is okay, but use of the C library memcpy() function would be more efficient:

            Point3d::Point3d( const Point3d &rhs )
            {
            ?? memcpy( this, &rhs, sizeof( Point3d );
            };
            Use of both memcpy() and memset(), however, works only if the classes do not contain any
            compiler-generated internal members. If the Point3d class declares one or more virtual functions
            or contains a virtual base class, use of either of these functions will result in overwriting the
            values the compiler set for these members. For example, given the following declaration:

            class Shape {
            public:
            ?? // oops: this will overwrite internal vptr!
            ?? Shape() { memset( this, 0, sizeof( Shape ));
            ?? virtual ~Shape();
            ?? // ...
            };
            the compiler augmentation for the constructor generally looks like this:

            // Expansion of constructor Pseudo C++ Code
            Shape::Shape()
            {
            ?? // vptr must be set before user code executes
            ?? __vptr__Shape = __vtbl__Shape;

            ?? // oops: memset zeros out value of vptr
            ?? memset( this, 0, sizeof( Shape ));
            };
            As you can see, correct use of the memset() and memcpy() functions requires some knowledge of the
            C++ Object Model semantics! 嘿, 把C庫(kù)扯進(jìn)來了, 強(qiáng)! C庫(kù)中許多強(qiáng)調(diào)性能,效率的函數(shù)是用匯編寫的

            Summary: 編譯器盡可能地"優(yōu)化掉"拷貝構(gòu)造函數(shù), 代之以NRV...
            ---------------------------------------------------------
            Application of the copy constructor requires the compiler to more or less transform portions of
            your program. In particular, consider a function that returns a class object by value for a class
            in which a copy constructor is either explicitly defined or synthesized. The result is profound
            program transformations both in the definition and use of the function. Also, the compiler
            optimizes away the copy constructor invocation where possible, replacing the NRV with an additional
            first argument within which the value is stored directly. Programmers who understand these
            transformations and the likely conditions for copy constructor optimization can better control the
            runtime performance of their programs.

            posted on 2006-05-11 03:33 Jerry Cat 閱讀(583) 評(píng)論(0)  編輯 收藏 引用

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



            <2006年5月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            留言簿(7)

            隨筆檔案

            最新隨筆

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            a高清免费毛片久久| 亚洲AV无码成人网站久久精品大| 69国产成人综合久久精品| 久久99亚洲网美利坚合众国| 青青草国产精品久久| 久久久久国色AV免费观看| 亚洲va中文字幕无码久久不卡| 好久久免费视频高清| 亚洲人AV永久一区二区三区久久| 久久国产亚洲高清观看| 日产久久强奸免费的看| 久久久国产精品网站| 伊人热热久久原色播放www| 好属妞这里只有精品久久| 中文无码久久精品| 久久国产高清一区二区三区| 精品国产福利久久久| 少妇高潮惨叫久久久久久| 人妻精品久久久久中文字幕| 久久免费国产精品一区二区| 亚洲国产精品成人久久| 青青草国产97免久久费观看| 青青草国产精品久久久久| 久久精品中文字幕无码绿巨人| 伊人久久大香线蕉综合网站| 99久久99久久精品国产| 久久99国产精品尤物| 亚洲中文字幕久久精品无码喷水| 美女久久久久久| 久久久久18| 久久天天躁狠狠躁夜夜av浪潮| 日韩一区二区久久久久久| 无码精品久久久天天影视| 久久婷婷五月综合97色| 欧美噜噜久久久XXX| 久久精品一本到99热免费| 久久精品国产亚洲av水果派| 久久婷婷成人综合色综合| 国产精品美女久久久m| 99麻豆久久久国产精品免费| 97久久天天综合色天天综合色hd |