[Turn] "Deep Exploration C + + object Model" Reading notes [II]

Source: Internet
Author: User
Tags array definition

Access to the 3.3 Data member
1. In any case, each static data member has only one entity, placed in the program's data segment, each time the program takes a static member, whether through Operator:: or member selection The operator is internally converted into a direct reference operation to the unique extern entity. The access of each static member and the association with class does not result in any additional execution time or space. If there are two classes, each of which declares a static member FreeList, then when they are all placed in the program's data segment, it causes name collisions, and the compiler solves the problem by using name-mangling, Secretly encode each static data member to obtain a unique program identification code.

2. How many compilers there are, and how many name-mangling practices there are, there are two points to any name-mangling approach:

ü An algorithm to derive a unique name;

ü If the compilation system or environment tool must talk to the user, those unique names can easily be deduced back to their original names.

3. Taking the address of a static data member, you get a constant pointer to its data type, not a pointer to its class member.

4. Nonstatic data members are placed directly in each class object, except for the displayed explicit or implied implicit class object, which cannot be accessed directly. As long as the programmer processes a nonstatic data member directly in a member function, the so-called implicit class object will occur, essentially the compiler will add a const to this member function The this pointer, while inside the function body, accesses the Nontatic data member through the this pointer.

5. To access an nonstatic data member, the compiler needs to add the start address of class object to the compilation offset of the data member, such as address &someobject.somemember equals &someobject + (&theclass::somemember–1), a pointer to the data member, whose offset value is always added to 1, which allows the compilation system to differentiate a pointer to the first data of class A pointer to member and a pointer to no data member.

6. The offset of each nonstatic data member is known at compile time, and even if the member belongs to a single or multiple inheritance system, the base class Subobject is the same, so its access efficiency and a C struct The access efficiency of member or a nonderived class member is the same. But in the case of virtual inheritance, it is another matter: if the nonstatic data member is a virtual base class member, and accessed by pointers, it will not be known at compile time that this member true offset location, Therefore, this access operation must be deferred to the execution period, which can be resolved by an additional indirect guidance.

2002-7-7

3.4 "Inherit" with data Member
1. In the C + + inheritance model, what a derived class object shows is the sum of its own members plus its base classes members. C + + does not specify the order of derived class members and base classes members. However, on most compilers, the base class members always appear first, except for the virtual base class.

2. In general, the specific inheritance of concrete inheritance does not increase the additional burden on space or access time.

3. It is easy to make two mistakes by making up the two independent unrelated classes into a pair of type/subtype, and with the inheritance relationship. One is the possibility of repeating the design of some functions of the same operation, in general, the selection of some functions into the inline function is an important topic in the design class, and the second is to decompose a class into multiple layers, it is possible to express the abstraction of the class system, because the compiler's boundary adjustment and expansion of the space required. The root cause of this is that C + + guarantees that the base class Subobject appearing in the derived class has its full intact nature.

4. When C + + was first introduced, many compilers placed vptr on the end of class object, preserving the object layout of the base class C struct. Since then, some compilers have begun to place vptr at the beginning of class object, which will give some help to calling virtual function under multiple inheritance by pointers to class members, otherwise, the execution period must not only be prepared from the class Offset at the beginning of object, and offset between class vptr must be prepared.

5. Single inheritance provides a natural polymorphic form of transformation between the base type and the derived type in the class system. In general, both the base class and the derived class objects start from the same address. However, if the vptr is placed at the beginning of class object, if the base class has no virtual function and derived class has, then the natural polymorphism of the single inheritance will break. In this case, converting a derived object to its base type requires the compiler's intervention to adjust the address. In the case of multiple inheritance and virtual inheritance, the compiler's intervention is more necessary.

6. The complexity of multiple inheritance lies in the unnatural relationship between the derived class and its previous base class and even the previous base class, whose main problem occurs in derived class objects and its second or successor base class The conversion between objects. For a multiple-derived object, assign its address to the pointer to the leftmost base class, and the case will be the same as a single inheritance, while the second or subsequent base class address specifies that the operation needs to modify the address, plus or minus (if downcast) a middle base class The size of the subobjects. C + + does not require multiple inheritance derived class object in the order of each base class Subjectes, and now each compiler is in accordance with the order of declarations to arrange them.

7. If one or more virtual Bass class subobjects are included in class, they will be split into two parts: an invariant part and a shared part. Invariant local always has fixed offset, its data to specify the location of the shared local, can be directly accessed, while the share of the local performance is the virtual base class Subobject, its location will be due to the derivation of each operation and can only be indirectly accessed. The difference between compiler implementations is that the method of indirect access is different.

8. In general, one of the most effective ways to use virtual base class is an abstract class that does not have any data member.

2002-7-14

3.5 Efficiency of Object members
If the optimization switch is not turned on, it is difficult to guess the efficiency of a program performance, because the program code is potentially affected by some compiler-related things. If a programmer cares about efficiency, he should actually test it, not by inference or common sense judgment or hypothesis. Optimization operations do not always work effectively.

2002-7-15

3.6 Pointers to data members
Pointers to data members can be used to investigate the underlying layout of class members in detail, and can be used to determine whether the vptr is placed at the beginning or end of a class, and to determine the order of access sections in class.

Take a nonstatic data member address, will get its offset in class, and take a static data member address or take a binding to the real class object on the body of the data member address, Will get the real address of the member in memory. This is also the SomeType SomeClass: The potential difference between:* and Sometye *.

2002-7-16

function semantics the semantics of function
C + + supports three types of member Functions:static, nonstatic and virtual, each of which is called differently.

4.1 Members ' various invocation methods
1. One of the design guidelines for C + + is that nonstatic member function must be at least as efficient as the general nonmember function. Inside the compiler, the member function entity is converted to a peer nonmember function entity with the following steps:

ü Rewrite the function prototype signature to insert an additional parameter, this to the member function, so that class object can call it. Where this is a const pointer, if the function is const, then the result that is reflected on this is the data of this point is also const;

ü Each access to the nonstatic data member is accessed via the this pointer;

ü member function is re-written as an external function, the name of the functions mangling processing;

Thereafter, each function invocation operation must also be converted to provide the corresponding arguments.

2. Internal conversion steps for virtual functions: If normalize is a virtual member function,ptr->normalize (), it is internally converted to (*ptr->vptr[t]) (PTR); In fact, the vptr name is also mangled because there may be multiple vptrs;t that are the index value of the Vitrual table slot, associated to the normalize function, and the second PTR representing the this pointer.

3. Use class scope operator to explicitly call a vitual function, or call a vitual function through a class object its resolution will be the same as the nontatic member function! Therefore, an inline function entity of virtual function can be extended, thus providing great efficiency benefits.

4. The main feature of the static member function is that there is no this pointer, which causes it to not directly access the nonstatic members in its class, cannot be declared as const, volatile, or virtual, and does not need to pass the class Object to invoke. The static member function is presented outside the class declaration and given a mangled appropriate name. If you take the address of a static member function, it will be the address in memory, its address type is not a pointer to class member function, but a nonmember function pointer. An unexpected benefit of the static member function is that it can be a callback and can be successfully applied to the thread function.

2002-07-17

4.2 Virtual Member Functions dummy member function
1. In C + +, a polymorphic polymorphism represents a derived class object addressed as a public base class pointer or reference. Identify whether a class supports polymorphism, and the only appropriate way to see if it has any virtual function. As long as the class has a virtual function, it needs an additional execution-time type of judgment information.

2. A class will only have a virtual table containing the address of the function entity that corresponds to all active virtual functions in class object. These active virtual functions include:

ü A class-defined function entity. It will rewrite overriding a possible base class virtual function.

ü Inherits the function entity from base class. The class does not overwrite the base class virtual function at this time.

ü A pure_virtual_called () function entity, which can act as a space defender for pure virtual functions, or as an exception-handling function at execution time. If the function is called, the usual action is to end the program.

3. Each virtual function is assigned a fixed, constant index value that remains associated with a particular virtual function throughout the inheritance system. This makes it possible to set virtual function calls at compile time.

2002-7-20

4. Under multiple inheritance, a derived class with an upper Basse classes number of N will contain n-1 additional virtual tables. The primary entity is shared with the leftmost base class, which contains all virtual Functios addresses, and n-1 secondary entities are related to other base classes, which contain only virtual objects that appear in the corresponding base class. The address of the functions.

5. Virtual function is supported in multiple inheritance, and its complexity is centered on the second and subsequent base class, and on the execution period this pointer adjustment. The second (or successor) base class affects the 3 scenarios supported for virtual function:

ü Call the derived class virtual function by pointing to the second base class pointer;

ü Invokes a inherited virtual function from the second base class by pointing to the derived class pointer;

ü Allows the return value type of the virtual function to vary, either base type or publicly derived type.

6. The effective solution to this pointer adjustment for the execution period is thunk. The so-called thunk is a small end assembly code used to adjust the this pointer with the appropriate offset value and jump to the corresponding virtual function. Thunk technology allows the virtual table slot to continue to contain a simple pointer, at which point multiple inheritance will not require any additional space burdens! The address in the slots can point directly to virtual function or to a related thunk.

4.3 Performance of the function
The nonmember, static member, and nonstatic member function are transformed internally into exactly the same form, and the three are equally efficient.

2002-08-08

4.4 Pointer to member function
For an nonstatic member function, the address of the function in memory is obtained, and in the face of a virtual function, an index value is obtained. This value is incomplete and must be bound to a class object to be able to invoke the function through it. The declaration syntax of a pointer to the member function, and a pointer to the member selection operator, which acts as the space reservation for the this pointer. Therefore, the type of the static member function is a pointer to the pointer, not to the member function.

Using a member function pointer is not as expensive as using a nonmember function pointer if it is not used for virtual function, multiple inheritance, virtual base class, and so on.

4.5 Inline Functions
The keyword inline is just a request. If, at some level, the execution cost of a function is lower than that of a normal function call and return mechanism, then the request is accepted and the compiler uses an expression to extend the function reasonably. The true inline function extension operation is at the point where the function is called. During the inline extension, each form parameter is replaced by the corresponding actual parameter, and each local variable in the inline function must be placed in a closed section of the function call with a unique name. This leads to the evaluation of parameters and the management of temporary objects.

2002-08-11

Construction, deconstruction, copy semantics semantics of construction, destruction, and copy
1. In general, the data member of class should be initialized and initialized only in constructor or other member functions, and any other operation will break its encapsulation nature, making its maintenance and modification more difficult.

2. Invoke a pure virtual function can be defined and invoked, but can only be called statically and cannot be invoked through a virtual mechanism. Each derived class destructor is extended by the compiler, statically invoking each virtual base class and the destructor of the previous base class. Therefore, regardless of whether the base class virtual destructor is declared as pure, it must be defined.

5.1 Object constructs without inheritance
C + + standard requires the compiler to delay the actual composition of the nontrivial members until they actually encounter their usage site.

5.2 Object construction under the inheritance system
In general, the compiler's expansion operations and order for constructor under the inheritance system are approximately as follows:

ü All virtual base class constructors must be called from left to right, from deep to shallow: If class is listed in the member initialization list, then any explicitly specified arguments must pass through, otherwise if CL There is a default constructor, which should also be called, and the offset of each virtual base class Subobject in class must be accessible at the execution time, if class object is the class of the lowest most-derived, its constructors may be called, and some mechanisms used to support this behavior must be in-party.

ü Call the previous base class constructors in the declaration order of base class: If the base class is listed in the member initialization list, then any explicitly specified arguments must pass through, otherwise if it If there is a default constructor or default Memberwise copy constructor, then it is called, and if base class is the second or successor base class under multiple inheritance, the this pointer must be adjusted.

ü If class object has virtual table pointer (s), it must be set to an initial value, pointing to the appropriate virtual table (s).

ü If there is a member that does not appear in the member initialization list, but it has the default constructor, call it.

ü The initialization of the data members in the member initialization list is put into the constructor function itself in the order in which the members are declared.

2002-8-18

5.3 Object Copy Semantics object copy semantics
1. It is only necessary to design a copy assignment operator if the semantics caused by the default behavior is unsafe or incorrect so that an alias aliasing or memory leak leak is occurring. Otherwise, the program will perform slowly instead.

2. If you are only providing a copy constructor to open the NRV optimization switch, then there is no need to provide a copy assignment operator.

3. Copy assignment operator has a non-orthogonal condition, which is that it lacks a member assignment list parallel to the member initialization list. Call the base class copy assignment operator Example:

Point::operator = (p3d); or (* (point*) this) = P3d; or (Point &) (*this) = P3d;

4. In fact, copy assignment operator behaves poorly in the case of virtual inheritance and needs to be carefully designed and explained. Many compilers do not even attempt to get the correct semantics, and they invoke each base class instance in the copy assignment operator of each medium, resulting in virtual base copy assignment Multiple entities of the operator are called. It is recommended that you do not allow a copy operation of the virtual base class as much as possible, and do not declare data member in any virtual base class.

5.5 Deconstruction semantics semantics of destruction
If class does not have a destructor defined, the compiler will automatically synthesize a destructor only if the member object or base class has destructor. A programmer-defined destructor is extended in a manner similar to the way constructors is extended, just in reverse order:

The function ontology of Üdestructor is executed first;

ü If class has member class objects and the latter has destructors, then they will be called in the opposite order of the Declaration;

ü If an object has a vptr in it, it is now reset to point to the appropriate base class virtual table;

ü If any direct nonvirtual base classes have destructor, they will be invoked in the opposite order of the Declaration;

ü If any virtual base classes has destructor, and the class previously discussed is Most-derived class, they will be called in the reverse order of the original construction order.

2002-8-19

Implementation-time semantics Runtime semantics
Construction and deconstruction of 6.1 objects
1. In general, the placement of constructor and destructor is as you would expect. But if there is more than one exit point in a section or function, the situation becomes more complicated and destructor is placed before each departure point. In general, we require that the object be placed as close to the area of the program where it is used, which can save unnecessary object generation and destruction operations.

2. All global objects in a C + + program are placed in the program's data segment, and if the initial value is not explicitly specified, the memory content configured by object will be 0 (C does not automatically set the initial value). If global object has constructor and destructor, we say it requires static initialization and memory release operations.

2002-8-20

3. The location of virtual base class Subobject in each derived class is subject to change and cannot be determined at compile time. Accessing the virtual base class Subobject as a derived class pointer or reference is a nonconstant expression that must be evaluated in the execution period.

4. There are some drawbacks to using the statically initialized object. One, cannot be put into the try section, any throw operation will trigger exception handling library default function terminate (); second, the programmer has to control "static initialization across modules" The complexity of the objects's dependency order comes at a cost. It is recommended that you do not use global objects that require static initialization at all.

5. The new C + + standard requires that the static local class objects in the compilation unit be constructed when the corresponding function is first called, and must be destroyed in the reverse order. Because these objects are constructed when needed, they cannot be expected to be set and ordered at compile time. To support the new standard, it is possible to maintain a list of execution periods for the generated static local class objects.

2003-8-1

6. For an object array definition, the recent compiler typically provides two functions for working with classes that do not have virtual base class, and classes with the virtual base class, which are often referred to as vec_new, Vec_vnew. The former type is usually:

void* vec_new (//initialization programmer does not provide a sequential element of the initial value

void *array,//array start address if 0, dynamic allocation

size_t elem_size,//size of each class object

int Elem_count,//number of elements in array

void (*constructor) (void *),//class's default constructor pointer

void (*destructor) (void *, char)//class destructor pointer, filled in 0

); If the programmer supplies the default constructor with defaults, the compiler will do special processing to pass in the default parameter values!

The two functions corresponding to destroying an array are vec_delete, Vec_vdelete, respectively. The former type is usually:

void* Vec_delete (

void *array,//array start address

size_t elem_size,//size of each class object

int Elem_count,//number of elements in array

void (*destructor) (void *, char)//class destructor pointer

);

6.2 New and DELETE operators
Note the distinction between operator new and new operator! The former is responsible for allocating memory, which first calls the former to allocate memory and then calls constructor to implement initialization.

Finish

[Turn] "Deep Exploration C + + object Model" Reading notes [II]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.