C++对象模型
这是本书的部分读书笔记
关于对象
构造函数语意学
2.1Default Constructor的构建操作
带有Default Constructor的Member Class Object
带有Default Constructor的Base Class
1.derived class派生自一个带有default constructor的based calss:调用上一层的default constructor(根据他们的声明次序)
2.设计者提供多个constructor,但其中没有default constructor:编译器会将default constructor扩张进每一个construcor中
带有一个Virtual Function的Class
class继承一个或多个中有一个virtual constructor
带有一个Virtual Base Class 的 Class
2.2拷贝构造函数的构建
用一个对象去初始化另一个对象
将一个对象作为参数传进函数
将一个对象作为函数的返回值
默认成员逐一初始化
位逐次拷贝
不要位逐次拷贝
1.当class内含一个member object,其中有一个拷贝构造函数
2.当class继承的class存在拷贝构造函数
3.class声明一个或多个virtual functions
4.class继承自一个或多个virtual class
重新设定Virtual Table的指针
增加一个virtual function table,内含每一个有作用的virtual function地址
???Handling the virtual base calss subobject
2.3 Program Transformation Semantics
Explicit Initialization
1.Each definition is rewritten with the initialization stripped out
2.An invocation of the class copy constuctor is inserted.
Argument Initialization
Return Value Initialization
1.add an additional argument of type reference to the class object.
2.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.
optimization at the user level
The copy constructor:to have or have not?
2.4 Member Initialization List
The Semantics of Data
Class have different size:
1.Language support overhead.
2.Complier optimization of recognized special cases.
3.Alignment constraints.
3.1 The Binding of a Data Member
consider when the data member will be binded.
3.2 Data Member Layout
3.3 Access of a Data Member
Static Data Member:
A single instance of each data class static data member is stored within the data segment of the program.
Nostatic Data Member:
When the class is derived class containing a virtual base class within its inheritance hierarchy and the member being access is an inherited member of that virtual base class,the resolution must be delayed util runtime through an additional indirection.
3.4 Inheritance and the Data Member
Inheritance without Polymorphism
1.如果是用继承的话,子类的数据存储空间是放在父类存储空间的填补空间之后
2.上述的目的是为了保证子类拷贝给父类时members不被破坏
Adding Polymorphism
1.Placing the vptr at the end of the class object preserves the object layout of the base class C struct,thus permitting its use within C code.
2.Placing the vptr at the start of the class iS more efficient in supporting some virtual function invocations through pointers to class members under multiple inheritance.
Multiple Inheritance
需要调整数据存储的顺序
Virtual Inheritance
A pointer to each virtual base class is inserted within each derived class object
3.5 Object Member Efficiency
1.For single access,the otherwise reasonable stragegy of placing the variable's address within register significantly adds to the cost of the expression.
2.编译器很难识别继承而来的非多态对象
3.6 Pointer to Data Members
this may be old.
Function语意学
4.1 Varieties of Member Invocation
Nostatic Member function
1.Name Mangling
2.重新写成一个外部的函数
Virtual Member Functions
利用指针与索引值找到函数
Static Member Functions
Static Member Functions by being this-less and therefore have the same type as an equivalent nonmember function.
4.2 Virtual Member Functions
use Virtual table
多重继承(MI)下的 Virtual Functions
Use several virtual table.
Virtual Functions under Virtual Inheritance
4.3 Function Efficiency
Inline Member优化之后效率高的原因是编译器将“视为不变的表达式”提到循环之外,因此只计算一次。
4.4 Pointer-to-Member Functions
Supporting Pointer-to-Virtual-Member Functions
Pointer-to-Member Functions under MI
Pointer-to-Member Efficiency
4.5 Inline Functions
Formal Arguments
Local Variables
Semantics of Construction,Destruction,and Copy
Presence of a Pure Virtual Destructor
A better design alternative is to not declare a virtual destructor as pure.
Presence of a Virtual Specification
Presence of const within a Virtual Specification
leaving off the const
A Reconsidered Class Declaration
Object Construction without Inheritance
如果C++将object视为一个Plain ol' Data,将不会生成Default constructor等函数
Abstract Data Type
Preparing for Inheritance
5.2 继承体系下的对象构造
Virtual Inheritance
The Semantics of the vptr Initialization
执行期语意学
站在对象模型的尖端