|
Chapter 1: Objects lessons |
|
C++ object model |
|
Each class object in C++ has a pointer named vptr which points to a table that store all virtual function pointers this class |
|
has and a type_info object associated with this class in support of runtime type identification (RTTI) is also addressed |
|
within the virtual table, usually within the table's first slot. And all objects of this class share this virtual table, I |
|
think. |
|
Problem: it's still like this for new C++ (C11)? |
|
|
|
Inheritance |
|
In the case of virtual inheritance, only a single occurrence of the base class is maintained (called a subobject) |
|
regardless of how many times the class is derived from within the inheritance chain. |
|
?:Virtual inheritance is a technique used in object-oriented programming, where a particular base class in an inheritance hierarchy is declared to share its member data instances with any other inclusions of that same base in further derived classes. For example, if class A is normally (non-virtually) derived from class X (assumed to contain data members), and class B likewise, and class C inherits from both classes A and B, it will contain two sets of the data members associated with class X (accessible independently, often with suitable disambiguating qualifiers). But if class A is virtually derived from class X instead, then objects of class C will contain only one set of the data members from class X. |
|
Object model of base class instance: the data members of base class object are directly stored within the derived class |
|
object |
|
|
|
A keyword distinction |
|
Meta-language rule: when the language can't distinguish between a declaration and an expression, it is to be interpreted as a declaration |
|
int (*pf) (); //right declaration |
|
int ( *pf )( 1024 ); //wrong definition or declaration or invocation |
|
( *pf )( 1024 ); //right invocation |
|
struct == class except for the data member layout |
|
The data members within a single access section are guaranteed within C++ to be laid out in the order of their declaration. The layout of data contained in multiple access sections, however, is left undefined. The layout of data members of the base and derived classes is left undefined |
|
|
|
An object distinction |
|
three C++ programing paradigms: |
|
1. The procedural model as programmed in C, and, of course, supported within C++. |
|
2. The abstract data type (ADT) model in which users of the abstraction are provided with a set of operations (the public interface), while the implementation remains hidden. |
|
3. The object-oriented (OO) model in which a collection of related types are encapsulated through an abstract base class providing a common interface. |
|
|
|
Polymorphism |
|
Only the indirect manipulation of the object through a pointer or reference supports the polymorphism necessary for OO programming. Because in the ADT paradigm the programmer manipulates an instance of a fixed, singular type that is completely |
|
defined at the point of compilation. |
|
Library_materials thing1; |
|
Book book; |
|
|
|
//Oops: thing1 is not a Book! Rather, book is ``sliced'' — thing1 remains a Library_materials |
|
thing1 = book; |
|
|
|
The actual type of the object addressed is not resolved in principle until runtime at each particular point of execution ? |
|
?: Why, is we can't resolve it or we don't in C++? |
|
|
|
?: what's Nonpublic derivation in polymorphism ? |
|
|
|
The C++ language supports polymorphism in the following ways: |
|
1. throught a set of implict conversion, (i.e.) such as the conversion of a derived class pointer to the pointer of its public base class type: |
|
fruit *ft = new Apple(); |
|
2. through the virtual function mechanism: |
|
ft->eat(); |
|
3. through the dynamic_cast or typeid operators: |
|
if (Apple *ap = dynamic_cast<Apple *>(ft))... |
|
|
|
The memory requirments to represent a class object in general: |
|
1. the accumulated size of nonstatic data members |
|
2. plus any padding due to aligement constraints |
|
3. plus any internally generated overhead to support virtuals |
|
|
|
The type of a pointer instructs the compiler as to how to interpret the memory found at a particular address and also just how much memory that interpretation should span |
|
|
|
C++多态的原理: |
|
首先,类型的唯一作用在于帮助编译器确定所在内存位置存储数据的解释形式,是对内存正确解码的钥匙;同时编译器除了相信指针或者引用这把钥匙之外没有其他的线索去确定这一点,因此哪怕内存中实际存储的是类型A,但是编译器也只能把它解释成指针/引用类型X。编译器没法在静态的时候完全确定内存类型,因为函数调用的关系,编译器无法实现对传入的参数进行预估,而同时又保证多态的灵活性。但是这不妨碍利用指针/引用去实现多态,因为对对象实际方法的调用取决于虚表的内容,只要具有继承关系的类型具有相同的虚表入口地址,大家就可以公用同一个指针类型去实现不同的函数调用的相同代码和实现。但是C++的多态是不适用于对象的,对象会在初始化的过程中被编译器保证初始化成正确的虚表形式。对象是确定的。 |
|
|
|
Chapter 2. the semantics of constructor |
|
========================================= |
|
2.1 default constructor |
|
Compiler help to build a complete and necessary class initialization constructor. So if one class doesn't have a default |
|
constructor, compiler will create one for it and insert into code; if one class has a default construtor or other |
|
constructors that do not finish all initialization work (missing some member class object initialization work), |
|
compiler also help to argument these constructors and insert the necessary initialization work. |
|
|
|
2.2 Copy constructor |
|
Bitwise Copy Semantics |
|
|
|
2.4 Member initialization list |
|
The order in which the list entries are set down is determined by the declaration order of the members within the class |
|
declaration, not the order within the initialization list. |
|
And the initialization list entries are placed before explicit user code. |
|
|
|
Chapter 3. The semantics of data |
|
====================================== |