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, 那么它們會以其原來的構造順序相反的順序被調用.
?