notes of 《Inside the C++ object model》

来源:互联网 发布:淘宝店标网站 编辑:程序博客网 时间:2024/05/20 12:25
1,default constructor construction
A default constructor synthesiszes only when the implementation needs it.When the implementation needs it?
1.1,Member class object with default constructor.
1.2,Base class with default constructor.
1.3,Class with a virtual function.
1.4.Class with a virtual base class.

within the synthesized default constructor, only the base class subobjects and member class objects and
initialized. All other nonstatic data members, such as integers, pointers to integers, arrays of integers, and so on, are not initialized.
Programmers new to C++ often have two common misunderstandings:
1, That a default constructor is synthesized for every class that does not define one

2. That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class


2, copy constructor construction
In practice, a good compiler can generate bitwise copies for most class objects since they have bitwise copy semantics.
When are bitwise copy semantics not exhibited by a class? There are four instances:
2.1. When the class contains a member object of class for which a copy constructor exists(either explicitly 
declared by the class designer, as in the case of the previous String class, or synthesized by the compiler, as in the case of class Word)
2.2. When the class is derived form a base class for which a copy constructor exists(again, either explicitly declared or synthesized)
2.3. When the class declares one or more virtual functions
2.4. When the class is derived form an inheritance chain in which one or more base classes are virtual


3,program transformation semantics

Application of the copy constructor requires the compiler to more or less transform portion 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 explicit 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 transformation and the likely conditions for copy constructor optimization can better control the runtime 
performance of their programs.


4, member initialization list
the compiler iterates over and possibly reorders the initialization list to reflect the declaration order of the 
members. It inserts the code within the constructor body prior to any explicit user code.
4.1 static function characteristics:
4.1.1, It cannot directly access the nonstatic members of its class.
4.1.2, It cannot be declarec const, volatile, or virtual.
4.1.3, It does not need to be invoked througn an object of its class, although for convenience,it may.

4.2 Pointer-to-Member Functions
4.2.1 Use of a pointer to member would be no more expersive than a nonmember pointer to function if it weren't for 
virtual funcations and multiple inheritance(including,of course, virtual base class),which complicate both the type 
and invocation of a pointer to member.

5 Semantics of Construction, Desctruction, and Copy
In general,the data members of a class should be initialized and assigned to only within the constructor add other 
member functions of that class.To do otherwise breaks encapsulation, thereby making maintenance and modification of 
the class more difficult.
5.1 A better design is to not declare a virtual destructor as pure;
5.2 It is a bad design choice to decalre all functions virtual and to depend on the compliler to optimize away 
unnecessary virtual invocations.
5.3 Considerable more problematic is declaring a virtual fucntion const and then discovering that,in pratice,a 

derived instance really does need to modify a data member.In my code, I tend toward leaving off the const;

5.4 In C,global is treated as a tentative definition because it is without explicit initialization.A tentative
definition can occur multiple times within the program.While global is treated within C++ as a full definition.All 

global objects within C++ are treated as initialized.


5.5 the vptr is initialized  After invocation of the base class constructors but before execution of user-provided 

code or the expansion of members initialized within the member initialization list.


5.6 I recommend not permitting the copy operation of virtual base class whenever possible. An even stronger 

recommendation:Do not declare data within any class that serves as a virtual base class.


5.7 The cost of object construction and copy is measured as the Point3d class declaration increases in complexity 
as Plain Ol'Data, then as an abstract data type(ADT), and then as single, multiple, and virtual inheritances are incorporated in turn.

6.1 In general,place an object as close as possible to the code segment actually using it.Doing this can save you 

unnecessary object creation and destruction.


6.2 If the constructor of the object allocated using operator new throws an exception, the memory allocated is  released. 
原创粉丝点击