• <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>

            高山流水

            ----- 要黑就黑徹底

            Inside The C++ Object Model 學習筆記--Semantics of Construction, Destructin, and Copy

            Chapter 5. Semantics of Construction, Destruction, and Copy

            5.1 “無繼承” 情況下的對象構造
            1. 普通類型(和C相同)
            2. 抽相數據類型
            3. 為繼承作準備

            5.2 繼承體系下對象的構造
            1.? 通用繼承構造規則
            ???? (1) 在成員初始化列表中的data members初始化操作會被放進constructor的函數本身,并以members的聲明順序為順序。
            ???? (2) 如果有一個member沒有出現在初始化列表中,但它有一個default constructor,那么default constructor會被調用。
            ???? (3) 如果class object 有virtual table pointer(s), 它(們)必須被設定初始值,指向適當的virtual table(s)。
            ???? (4) 在那之前,所有的上一層的base class constructors必須被調用, 以base class的聲明順序為順序
            ???? (5) 在那之前,所有的virtual base class constructors必須被調用,從左到右,從最深到最淺。

            2. 虛擬繼承(virtual Inheritance)
            如對于下面的類:?

            ?1?class?Point3d?:?public?virtual?Point
            ?2??{?
            ?3??public:?
            ?4?????Point3d(?float?x?=?0.0,?float?y?=?0.0,?float?z?=?0.0?)?
            ?5?????????:?Point(?x,?y?),?_z(?z?)?{}?
            ?6?????Point3d(?const?Point3d&?rhs?)?
            ?7?????????:?Point(?rhs?),?_z(?rhs._z?)?{}?
            ?8?????~Point3d();?
            ?9?????Point3d&?operator=(?const?Point3d&?);?
            10?
            11?????virtual?float?z(){?return?_z;?}?
            12?????//??
            13??protected:?
            14?????float?_z;?
            15??};?

            ?
            ?可能的轉換是這樣的:

            ?1?//?Psuedo?C++?Code:?
            ?2??//?Constructor?Augmentation?with?Virtual?Base?class?
            ?3??Point3d*?Point3d::Point3d(?Point3d?*this,?bool?__most_derived,??float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???if?(?__most_derived?!=?false?)?
            ?6????this->Point::Point(?x,?y);?
            ?7?
            ?8???this->__vptr__Point3d?=?__vtbl__Point3d;?
            ?9???this->__vptr__Point3d__Point?=?__vtbl__Point3d__Point;?
            10?
            11???this->_z?=?rhs._z;?
            12???return?this;?
            13??}?

            ?//
            ?對于如下的繼承層次:
            ?class Vertex?? : virtual public Point { ... };
            ?class Vertex3d : public Point3d, public Vertex { ... };
            ?class PVertex? : public Vertex3d { ... };

            ?類Point3d的構造可能是:

            ?1?//?Psuedo?C++?Code:?
            ?2??//?Constructor?Augmentation?with?Virtual?Base?class?
            ?3??Point3d*?Point3d::Point3d(?Point3d?*this,?bool?__most_derived,??float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???if?(?__most_derived?!=?false?)?
            ?6????this->Point::Point(?x,?y);?
            ?7?
            ?8???this->__vptr__Point3d?=?__vtbl__Point3d;?
            ?9???this->__vptr__Point3d__Point?=?__vtbl__Point3d__Point;?
            10?
            11????this->_z?=?rhs._z;?
            12???return?this;?
            13??}

            ?//
            ?對于Vertex3d的構造可能是如下:

            ?1?//?Psuedo?C++?Code:?
            ?2??//?Constructor?Augmentation?with?Virtual?Base?class?
            ?3??Vertex3d*?Vertex3d::Vertex3d(?Vertex3d?*this,?bool?__most_derived,?float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???if?(?__most_derived?!=?false?)?
            ?6????this->Point::Point(?x,?y);?
            ?7?
            ?8???//?invoke?immediate?base?classes,?
            ?9???//?setting?__most_derived?to?false?
            10?
            11???this->Point3d::Point3d(?false,?x,?y,?z?);?
            12???this->Vertex::Vertex(?false,?x,?y?);?
            13?
            14???//?set?vptrs?
            15???//?insert?user?code?
            16?
            17???return?this;?
            18??}?

            ?

            3. vptr初始化語意學(The Semantics of the vptr Initialization)
            ???? (1) 構造函數執行算法
            ????????? I.??? 在derived class constructor 中, 所有的"virtual base classes" 及 "上一層base class"的constructors會被調用.
            ?? II.? 上述完成之后, 對象的vptr(s)會被初始化, 指向相關的virtual talbe(s).
            ?? III. 如果有成員初始化列表的話, 將在constructor體內擴展開來. 這必須在vptr被設定之后才能進行,以免有一個virtual member function被調用
            ?? IV. 最后, 執行程序所提供的代碼.
            ????
            ???? (2) 示例:
            ???? For example, given the following user-defined PVertex constructor:

            1?PVertex::PVertex(?float?x,?float?y,?float?z?)?
            2?????:?_next(?0?),?Vertex3d(?x,?y,?z?)
            3?????,?Point(?x,?y?)?
            4??{?
            5???if?(?spyOn?)?
            6????cerr?<<?"within?Point3d::Point3d()"??<<?"?size:?"?<<?size()?<<?endl;?
            7??}?

            ?

            ??可能一個擴展如下:

            ?1?//?Pseudo?C++?Code?
            ?2??//?expansion?of?PVertex?constructor?
            ?3??PVertex*?PVertex::PVertex(?Pvertex*?this,??bool?__most_derived,?float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???//?conditionally?invoke?the?virtual?base?constructor?
            ?6???if?(?__most_derived?!=?false?)?
            ?7????this->Point::Point(?x,?y?);?
            ?8???//?unconditional?invocation?of?immediate?base?
            ?9????this->Vertex3d::Vertex3d(?x,?y,?z?);?
            10?
            11???//?initialize?associated?vptrs?
            12?
            13???this->__vptr__PVertex?=?__vtbl__PVertex;?
            14???this->__vptr__Point__PVertex?=?__vtbl__Point__PVertex;?
            15?
            16???//?explicit?user?code?
            17???if?(?spyOn?)?
            18????cerr?<<?"within?Point3d::Point3d()"?
            19????<<?"?size:?"?<<?(*this->__vptr__PVertex[?3?].faddr)(this)?
            20????<<?endl;?
            21?
            22???//?return?constructed?object?
            23???return?this;?
            24??}?

            ?


            5.3 對象的復制(Object Copy Sematics)
            1. Copy constructor operator 在以下幾種情況下不會表現出: bitwisecopy
            ???? (1) class 內帶有一個member object, 而其類有一個copy constructor operator時。
            ???? (2)? 當一個 class 的base 有一個copy assignment operator時。
            ???? (3)? 當類聲明了任何一個virtual functions時。
            ???? (4)? 當class繼承自一個virtual base class時
            ?2. 合成示例

            ?1??//?Pseudo?C++?Code:?synthesized?copy?assignment?operator?
            ?2??inline?Point3d&?Point3d::operator=(?Point3d?*const?this,?const?Point3d?&p?)?
            ?3??{?
            ?4???//?invoke?the?base?class?instance?
            ?5???this->Point::operator=(?p?);?
            ?6?
            ?7???//?memberwise?copy?the?derived?class?members?
            ?8???_z?=?p._z;?
            ?9???return?*this;?
            10??}?

            ?

            5.4 對象的功能


            5.5 析構語意學(Semantics of Destruction)
            1. 析構函數生成原則:
            ?如果類沒有定義destructor, 那么只有在class內帶的member object(或是class自己的base class)擁有destructor的情況下,編譯器才會自動的合成一個來。否則,destructor會被視為不需要,也就不需要被合成(當然更不需要被調用)

            2. 析構調用過程
            ???? (1) destructor的函數本身首先被執行。
            ???? (2) 如果class擁有member class objects, 而后者擁有destructors, 那么它會以其聲明順序的相反順序被調用.
            ???? (3) 如果object內帶一個vptr, 則現在被重新設定,指向適當的base class的virtual table.
            ???? (4) 如果任何直接的(上一層)novirtual base classes 擁有destructor,那么它會以其聲明順序相反的順序調用
            ???? (5) 如果有任何的virtual base classes 擁有destructor, 而當前討論的這個class是最末端的class, 那么它們會以其原來的構造順序相反的順序被調用.

            ?

            posted on 2006-10-30 14:42 猩猩 閱讀(235) 評論(0)  編輯 收藏 引用 所屬分類: C&C++語言

            亚洲午夜精品久久久久久浪潮| 99久久精品国产综合一区| 免费久久人人爽人人爽av| 久久综合色之久久综合| 中文字幕无码免费久久| 久久99国产精品一区二区| 天天做夜夜做久久做狠狠| 亚洲中文字幕无码久久2020| 青青草国产精品久久| 一本久久综合亚洲鲁鲁五月天| 国产精品对白刺激久久久| 国产精品亚洲综合专区片高清久久久 | 久久久久久久综合综合狠狠| 久久国产AVJUST麻豆| 久久99国产精品一区二区| 精品久久亚洲中文无码| 99久久国产综合精品成人影院| 久久久久久久97| 欧美粉嫩小泬久久久久久久 | 久久精品中文闷骚内射| 久久综合色之久久综合| 66精品综合久久久久久久| 婷婷五月深深久久精品| 模特私拍国产精品久久| 久久久久18| 老司机国内精品久久久久| 久久久久久夜精品精品免费啦| 久久91精品国产91久| 久久亚洲精品无码观看不卡| 欧美一区二区精品久久| 狠狠色婷婷综合天天久久丁香 | 久久精品成人欧美大片| 亚洲精品久久久www| 久久精品免费大片国产大片| 久久综合欧美成人| 国产精品久久一区二区三区| 亚洲精品tv久久久久久久久| 久久丫精品国产亚洲av| 久久精品中文字幕无码绿巨人| 蜜臀久久99精品久久久久久小说| 久久久久高潮综合影院|