其实大多是一些摘录。第6章以后的内容就有点看不下去了,功力太差。
2.1 Default Constructor Construction
Consider the case of each constructor defined for a class containing one or more member class objects for which a default constructor must be invoked. In this case, the compiler augments the existing constructors, inserting code that invokes the necessary default constructors prior to the execution of the user code.
What if the designer provides multiple constructors but no default constructor? The compiler augments each constructor with the code necessary to invoke all required default constructors. However, it does not synthesize a default constructor because of the presence of the other user-supplied constructors. If member class objects with default constructors are also present, these default constructors are also invoked after the invocation of all base class constructors.
by inserting a pointer to each of the virtual base classes within the derived class object. All reference and pointer access of a virtual base class is achieved through the associated pointer. For each constructor the class defines, the compiler inserts code that permits runtime access of each virtual base class. In classes that do not declare any constructors, the compiler needs to synthesize a default constructor.
2.2 Copy Constructor Construction
There are only 3 ways to use copy constructor function: explicitly use =, function parameter by value and return by value.
When are bitwise copy semantics not exhibited by a class? There are four instances:
- When the class contains a member object of a 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)
- When the class is derived from a base class for which a copy constructor exists (again, either explicitly declared or synthesized)
- When the class declares one or more virtual functions
- When the class is derived from an inheritance chain in which one or more base classes are virtual
Handling the Virtual Base Class Subobject
2.3 transformation(may happens in most good c++ imp)
Argument Initialization:
X xx; foo( xx ); -> X xx;X __temp0; __temp0.X::X (xx); foo( __temp0 );
and the declaration of foo() should change:
void foo(X x0) -> void foo(X& x0)
Return Value Initialization:
change the declaration of function, add return value as a parameter by reference, init before function call, and invoke copy constructor at the end of function, for example:
X bar()
{
X xx;
// process xx ...
return xx;
}
X xx = bar();
->
void bar( X& __result )
{
X xx;
xx.X::X();
__result.X::X( xx );
return;
}
X xx; bar( xx );
2.4 Member Initialization List
You must use the member initialization list in the following cases in order for your program to compile:
1. When initializing a reference member
2. When initializing a const member
3. When invoking a base or member class constructor with a set of arguments
4. use member object’s constructor in member in it list, instead of use copy constructor in body explicitly, which avoid the expense of object create and delete
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
3.3 Access of a Data Member
A single instance of each class static data member is stored within the data segment of the program. Each reference to the static member is internally translated to be a direct reference of that single extern instance.
Access of a nonstatic data member requires the addition of the beginning address of the class object with the offset location of the data member.
&origin._y; is equivalent to the addition of
&origin + ( &Point3d::_y - 1 );
access is significantly different when the Point3d class is a derived class containing a virtual base class within its inheritance hierarchy and the member being accessed, such as x, is an inherited member of that virtual base class. In this case, we cannot say with any certainty which class type pt addresses (and therefore we cannot know at compile time the actual offset location of the member), so the resolution of the access must be delayed until runtime through an additional indirection
Consider about what functions should be inlined is a very important issue in class design
3.4 Inheritance and the Data Member
Inheritance without Polymorphism:
The complier guarantees of the integrity of the base class subobject within the derived class. for example,
class Concrete {int val; char c1; char c2; char c3; }; is 8 bytes,
class Concrete1 { int val; char bit1; };
class Concrete2 : public Concrete1 { char bit2; };
class Concrete3 : public Concrete2 { char bit3; };
Concrete3 is 16 bytes
Virtual Inheritance:
A class containing one or more virtual base class subobjects, such as istream, is divided into two regions: an invariant region and a shared region. Data within the invariant region remains at a fixed offset from the start of the object regardless of subsequent derivations. So members within the invariant region can be accessed directly. The shared region represents the virtual base class subobjects. The location of data within the shared region fluctuates with each derivation. So members within the shared region need to be accessed indirectly.
a popular solution of the indirectly access is to keep virtual base class offset in virtual function table.
3.6 Pointer to Data Members
4.1 Varieties of Member Invocation
for nonmember function, nonstatic member function and static member function, there is a skill called name-mangling, function name is rewritten in compose of name, class and parameter. and an additional parameter will be added to non static function, that’s the this pointer of the class object.
virtual function invocation will be changed like this:
obj->virtual_function() à obj->vptr[1](ptr)
which 1 means the index of this virtual_function in vtbl
but remember only when called by a pointer will be treated like this, if called by an object, just in the name-mangling way like normal function.
Chapter 5. Semantics of Construction, Destruction, and Copy
The data member of a class should only be inited in constructor or member function of the very class, not in derived class function or others.
a link error will occours if no pure virtual destructor is defined for a virtual base class.
The import of virtual function should be accompanied by a virtual destructor.