Chapter II Semantics of tectonic functions the semantics of constructors
1. Jerry Schwarz,iostream, a library constructor, has defined a conversion operator operator INT () in order for CIN to get a true or false value. But in the statement Cin << Intval, its behavior is unexpected: The program originally wanted cout rather than CIN. But the compiler found a correct explanation: turn cin into an integral type, and now left shift operator << is ready to work. This is the so-called "Schwarz Error." Jerry finally replaces the operator INT () with operator void * ().
2. The purpose of introducing keyword explicit is to provide programmers with a way to prevent the constructor of a single parameter from being treated as a conversion operator. Its introduction is sensible, but its testing should be brutal.
Construction of 2.1 Default constructor
1. The Global objects memory guarantee is cleared at 0 when the program is activated. Local objects are configured on the program's stack, heap objects are configured in free space and are not necessarily cleared to be 0, and their content will be the remnants of memory last used.
2. "The default constructors is generated by the compiler when needed." The key word is "in time of Need". is required by the compiler.
For class X, if there is no user-declared constructor, then a default constructor is implicitly declared. An implicitly-declared default constructor will be a trivial (useless) constructor .....
3. In the following four cases, the compiler must synthesize a implicit nontrivial default constructor for classes that are not declared constructor:
(1) Member class object with default constructor
(2) base class with default constructor
(3) With virtual function
(4) with virtual base class
In all other situations and without declaring any constructor classes, they have implicit trival default constructors, which are not actually synthesized.
4. Member class object with default constructor
(1) in various versions of the module, the compiler avoids the process of synthesizing multiple default constructor as follows:
The synthesized default constructor, copy constructor, assignment copy operator are done in inline mode. A inline function has a static link that will not be seen by people outside the file. If the function is too complex to be made into a inline, a explicit non-inline static entity is synthesized.
(2)
The synthesized bar default constructor contains the necessary code to invoke the default constructor of class Foo to handle the member object Bar::foo, but it does not produce any code to initialize Bar:: Str.
It is the responsibility of the compiler to initialize the Bar::foo, and it is the programmer's responsibility to initialize the BAR::STR.
The synthesized default constructor only satisfies the compiler's needs, not the programmer's. In order for the program fragment to execute correctly, character pointer Str also needs to be initialized.
The compiler expands the existing constructor, placing some code in it, and before the user code is executed, invoke the necessary default constructors first.
(3) If more than one class member objects requires a constructor initialization operation, the C + + language requires that each constructors be called with the "Member objects in the order of declaration in class."
5. Base class with default constructor
If a class without any constructor derives from a base class "with default constructor", then the default of this derived class Constructor will be treated as nontrivial and therefore need to be synthesized. It will invoke the default constructor of the previous layer base classes (according to their declared order).
6, with a virtual function of class
In two cases, the default constructor also needs to be synthesized
(1) class declaration (or inheritance) of a virtual function
(2) class derives from a chain of inherited strings, which has one or more virtual base classes.
Because of the lack of constructors by the user declaration, the compiler details the necessary information to synthesize a default constructor.
In addition, the virtual invoke operation of the Widget.flip () is rewritten to use the flip () entry in the widget's vptr and VTBL:
(*widget.vptr[1]) (&widget)
Where: (1) 1 represents the fixed index of flip () in virtual table
(2) &widget represents the this pointer to "one of the called Flip () function instances".
7, with a virtual base class class
The virtual base class must be positioned in each derived class object to be ready for the execution period.
All operations that access a virtual base class via reference or pointer can be done with the relevant pointers.
Where _VBCX represents the pointer produced by the compiler, pointing to virtual base class X
The _VBCX (or something that the compiler makes) is done during class object construction. For each constructor defined by class, the compiler will insert code that "allows actuator access for each virtual base class". If class does not declare any constructors, the compiler must synthesize a default constructor for it.
Summarize:
There are four scenarios in which the compiler must synthesize a default constructor for the classes declared constructor. These compounds are referred to as implicit nontrivial default constructors. The synthesized constructor can only meet the needs of compilers (not programs).
Without the four cases and without declaring any constructor classes, we say that they have the implicit trivial default constructors, they are not actually synthesized.
In the synthesized default constructor, only the base class Subobjects and member class objects are initialized. All other nonstatic data member (such as integers, integer pointers, integer arrays, and so on) are not initialized. These initialization actions may be necessary for the program, but are not necessary for the compiler.
Novice misunderstanding:
(1) Any class that does not define the default constructor will be synthesized.
(2) The compiler's resultant default constructor will explicitly set the "defaults for each data member in class".
-It's all wrong.
8, compiler synthesis implicit nontrivial default constructor, but secretly did some important things to ensure that the program is properly and reasonably run. If the programmer provides multiple constructors, none of which has a default constructor, the compiler will also insert some code of the same functionality into these constructors, which will be placed before explicit user code.
2.2 Copy Constructor's construction operation
0, a class object can be copied from two ways: initialization and designation, conceptually, these two operations are done in copy constructor and copy assignment operator.
1, Default memberwise initialization
If class does not provide a explicit copy constructor then what if. When class object is initialized with "another object of the same class" as the initial value, the interior is done with the so-called default memberwise initialization, which is the values of each built-in or derived data member, Copy from one object to another object. However, he does not copy member class object, but rather recursively execute memberwise initialization.
The Default constructors and copy constructors are generated by the compiler when necessary. "Necessary" means when class does not show bitwise copy Semanics.
The standard for deciding whether a copy constructor is Trival is whether class shows the so-called "bitwise copy Semanics".
2, Bitwise copy semanics (bit successive copy)
This situation does not require a default copy constructor, as the above statement shows the "Default copy Semanics"
In this case, the compiler must synthesize a copy constructor to invoke the copy constructor of member class string object:
3. Don't "Bitwise copy Semantics"
In the following four cases, one class does not show bitwise copy semantics:
(1) class contains a member object whose class declaration has or is synthesized by the compiler there is a copy constructor;
(2) class inherits from a base class the latter exists or is synthesized by the compiler there is a copy constructor;
(3) When class declares one or more virtual functions;
(4) When class derives from a chain of inherited strings, which has one or more virtual base classes.
In the first two cases, the compiler must place the copy constructors invocation operation of member or base class into the synthesized copy constructor.
4, reset the virtual table pointer
(1) Once a class object must introduce Vptr, the compiler must correctly set the initial value for its vptr. At this point, the class will no longer show bitwise semantics.
(2) When a base class object initializes an operation with its derived class object content, its vptr copy operation must be secured.
5, processing virtual Base Class Subject
In every compiler's support commitment to virtual inheritance, it means that the "virtual base class Subobject location in derived class object" must be ready during the execution period. The integrity of the maintenance location is the responsibility of the compiler.
2.3 Program Conversion semantics
1. Each explicit initialization operation will have two necessary conversion stages: rewrite each definition first, strip the initialization, and then insert the copy constructor of class to invoke the operation.
2. Passing a class object as an argument to a function or as a return value of a function is equivalent to the following initialization operation:
X xx = arg; where xx represents the formal parameter or return value, and ARG represents the true parameter value.
3. The function is defined as follows: X Bar () {x xx; return Xx;},bar () The returned value is copied from the local object XX by a two-step transformation:
You first add an extra parameter for bar, which is a reference of class object, which is used to place the return value that was built by the copy.
U then place a copy constructor call operation before the return instruction so that the content of the object to be returned is treated as the initial value of the new parameter, while overriding the function so that it does not return any values.
4. Named return Value (NRV) optimization is now considered an obligatory optimization operation for the standard C + + compiler, characterized by direct manipulation of newly added additional parameters. Note that only the presence of copy constructor activates NRV optimizations for the C + + compiler. NRV optimization has greatly improved efficiency, but it has been criticized: first, the optimization by the compiler silently, and whether the completion and its completion is completely transparent; second, once the function becomes more complex, optimization becomes more difficult to execute; the third is that optimization can cause errors Sometimes the constructor and destructor are not called symmetrically, but the copy constructor is not invoked.
5. If the compiler provides NRV optimization, if it can be foreseen that class requires a large number of memberwise initialization operations, such as passing back objects by value, provide a explicit inline copy The constructor function entity is very reasonable. In such cases, it is not necessary to provide the explicit assignment operator definition at the same time.
6. The application of copy constructor forces the compiler to partially optimize program code, especially when a function returns a class object by value, and the class has a copy constructor (or is defined or synthesized). Both the definition of a function and the use of it will lead to esoteric program transformations. In addition, the compiler will implement NRV optimizations.
7. Note that the correct use of memset () and memcpy () is valid only if the classes does not contain any internal members such as VPTR generated by the compiler.
2.4 Member Initialization list
1. When you write down a constructor, you have the opportunity to set the initial value of the class members. Not through the member initialization list, but within the constructor function itself.
2. In the following cases, you must use the member initialization list in order for the program to be compiled successfully:
U initialization of a reference member;
U Initializes a const member;
U call a base class constructor, and it has a set of parameters;
You call the constructor of a member class, and it has a set of parameters.
3. The compiler will process and possibly reorder the initialization list one by one to reflect the order in which members are declared, and it will place some code into the constructor and precede any explicit user code.
4. One caveat: Use "one member in the constructor body" instead of "one member in the member initialization list" to set an initial value for another member.