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

Source: Internet
Author: User
Tags bitwise

Preface Stanley B.lippman
1. Three conversion flavors required for any object model:

ü Conversions that are closely related to the compiler

ü Language Semantics transformation

ü Conversion of program code and object model

2. Two interpretations of the C + + object model

ü Direct support for object-oriented programming in the language

ü The underlying implementation mechanism for various support

3. The complete virtual functions of C + + class is fixed at compile time, and programmers have no way to dynamically add or replace one of them during the execution period. This enables the virtual function call operation to have a fast delivery result, but the flexibility of the execution period is paid.

4. All compilers now implement virtual function using each class-specific virtual table, which is fixed in size and constructed before the program executes.

5. The underlying mechanism of the C + + object model is not standardized, and it differs depending on the implementation (compiler) and time.

2002-6-23

About Object Lessons
1.1 C + + object mode
1. The main additional burden of C + + in layout and access time is generated by virtual, including the virtual function mechanism and the virtual base class mechanism, as well as some occurrences in "a derived class and its second or subsequent base Multiple inheritance on the conversion of class.

2. In the C + + object model, nonstatic data members are configured within each class object, and the static data members are stored outside of all class object, static and nonstatic Function members are also placed outside of all class object, and virtual functions is supported in two steps: Each class produces a bunch of pointers to virtual functions, placed in virtual table ( VTBL), each class object is added with a pointer vptr to the associated virtual table. The Type_info object associated with each class is also indicated by VTBL, which is usually placed in the first slot of the VTBL. Vptr is automatically completed by each class's construtor, destructor, and copy assignment operator. The main advantage of the above model is the efficiency of space and access time, and the main disadvantage is that the application code must be recompiled as long as the nonstatic data members of the class object used by the application have been modified.

3. The inherited model used by C + + does not use any indirection, and the data members of base class Subobject are placed directly in the derived class object. The advantage is a compact and efficient access to the base class members, with the disadvantage that any changes to the base class members will cause the application code that uses its derived class object to be recompiled.

4. The original model of virtual base class is to add a pointer to each associated virtual base class in class object, and the other evolved model is not to import a virtual base class table, is to expand the existing VTBL to maintain the location of each virtual base class.

1.2 Differences in Keywords
1. It can be said that the use of the keyword struct is accompanied by a public interface declaration, it can also be used only to facilitate C programmers to migrate to C + + tribes.

2. Any data in C + + that is in the same access section must be guaranteed to appear in the memory layout in the order of declarations, but the order of the individual data placed in multiple access sections is not necessarily the same. Similarly, the layout of the data members of base classes and derived classes does not have the mandate of who first.

3. Combining composition instead of inheritance is the only viable way to combine C and C + +.

1.3 Differences in Objects
1. The C + + program Design model supports three program design paradigms programming Paradigms:

ü Program Models Procedural model

ü Abstract data type model, ADT

ü Object-oriented data model object-oriented Model,oo

2. Although a base class object in the inheritance system can be processed directly or indirectly, only through the indirect processing of pointer or reference can the polymorphic properties required for OO programming be supported.

3. In C + +, polymorphism exists only in the public class system, and the derived behavior of nonpublic and pointers of type void* can be said to be polymorphic, but they are not clearly supported by the language and must be managed by the programmer through the transformation operations shown.

4. C + + supports polymorphism in the following ways:

ü Through a set of implicit conversion actions, such as converting a derived class pointer to a pointer to its public base type;

ü Through virtual mechanism;

ü via the dynamic_cast and typeid operators.

5. The main use of polymorphism is to influence the encapsulation of types through a common interface, which is usually defined in an abstract base class. This interface is triggered by the virtual function mechanism, which can parse out which function entity is called in the execution period based on the true type of object.

6. The memory required for a class object is generally made up of the following parts:

Ünonstatic The sum size of the data members;

ü Any space that is filled up by alignment demand;

ü Any additional burden generated by the interior to support virtual.

7. A pointer or reference, regardless of which data type it points to, the amount of memory required by the pointer itself is fixed. Essentially, a reference is usually implemented as a pointer, and if the object syntax is converted to indirection, a pointer is required.

8. The differences that point to pointers of different types, neither in their pointer notation nor in their content, but in the type of object they address, that is, the pointer type teaches the compiler how to interpret memory content and size in a particular address. They support polymorphism because they do not cause any type-related memory delegate operations in memory, but are changed only by the size of the memory they point to and how the content is interpreted.

9. Transformation cast is actually a compilation instruction, and in most cases it does not change the actual address contained in a pointer, it only affects the size and content of the point that is directed to the memory.

10. When a base class object is directly initialized or specified as a derived object, the derived object is cut sliced to plug into the smaller base type memory and the polymorphic is no longer rendered. A strict compiler can avoid the virtual mechanism by parsing a virtual function call operation that is triggered by the object during compilation time. At this point, if virtual function is defined as inline, there will be an efficiency gain.

11. C + + supports polymorphism through class pointer and reference, a programming style called oo;c++ that also supports specific ADT style, which is now called object-based OB, does not support polymorphism, and does not support type expansion.

2002-6-25

constructor semantics the semantics of constructors
1. Jerry Schwarz,iostream, a library architect, had defined a conversion operator operator INT () to allow CIN to obtain a true and false value. In the statement CIN << Intval, however, the behavior is unexpected: The program originally wanted cout, not cin!. But the compiler found the right interpretation: to transform CIN into an integral type, now left shift operator << can work! This is called "Schwarz Error". Jerry finally replaced operator Int () with operator void * ().

2. The purpose of introducing keyword explicit is to provide programmers with a way to prevent constructor of a single parameter from being treated as a conversion operator. Its introduction is sensible, but its test should be brutal!

Construction operation of 2.1 Default constructor
1. The memory guarantee for global objects is cleared at 0 when the program is activated. The local objects is configured in the program's stack, and the heap objects is configured in free space, not necessarily cleared to 0, and their contents will be the remains of the memory that was last used.

2. In each of the different versions of the module, the compiler avoids synthesizing multiple default constructor methods: The synthesized default constructor, copy constructor, assignment copy The operator are all done in inline mode. An inline function has a static link and will not be seen by anyone other than the file. If the function is too complex to fit inline, a explicit non-inline static entity is synthesized.

3. In the following four cases, the compiler must synthesize a implicit nontrivial default constructor for classes that are not declared constructor: constructor class with default member Object, base class with default constructor, with virtual function, with virtual base class. In all other cases and without declaring any constructor classes, they have implicit trival default constructors, which are not actually synthesized.

4. The compiler synthesized implicit nontrivial default constructor, but secretly made some important things to ensure that the program runs correctly and reasonably. If the programmer provides multiple constructors, but none of them has the default constructor, the compiler will also insert code with the same functionality in these constructors, which will be placed before the explicit user code.

2002-6-26

Construction operation of 2.2 Copy constructor
1. There are three cases in which the content of one class is used as the initial value of another class object:

ü A definite initialization operation for an object, such as: SomeClass obt = OBTB;

ü An object is given as a parameter to a function, such as: foo (OBT);

ü When the function returns a class object.

If the Class Designer explicitly defines a copy constructor, in most cases the constructor will be called. This can lead to a temporary generation of class object or a metamorphosis of the program code, or both.

2. If class does not provide a explicit copy constructor, when class object is the initial value of another object of the same class, its internal is the so-called default memberwise The initialization technique, which is to copy the value of each built-in or derived data member from one object to another object. However, it does not copy the Member class object, but instead memberwise the initialization in a recursive manner.

3. A class object can be copied in two ways: Initialize and specify, conceptually, the two operations are done with copy constructor and copy assignment operator.

4. If class does not declare a copy constructor, there will be an implied declaration implicitly declared or implied definition implicitly defined appears. C + + The copy constructor is divided into trivial and nontrivial two kinds. Only nontrivial entities will be synthesized. The criterion for determining whether a copy constructor is trivial is whether the class shows the so-called "bitwise copy Semantics".

5. In the following four cases, a class does not show bitwise copy semantics:

Üclass contains a member object while the latter's class declaration has or is synthesized by the compiler with a copy constructor;

Üclass inherits from a base class that exists or is synthesized by a compiler with a copy constructor;

ü When class declares one or more virtual functions;

ü When class derives from an inherited string chain, where there is one or more virtual base classes.

In the first two cases, the compiler must place the copy constructors invoke operation of the member or base class into the synthesized copy constructor.

6. 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.

7. When a base class object is initialized with its derived class object content, its vptr copy operation must be secure.

8. Each compiler's commitment to virtual inheritance means that the location of the derived class Subobject in the base class object must be ready for the execution period. Maintaining the integrity of the location is the responsibility of the compiler.

2002-6-27

2.3 Program Transformation semantics
1. Each explicit initialization operation will have two necessary program transformation phases: Rewrite each definition, peel off the initialization, and then insert the class's copy constructor call operation.

2. Pass a class object as an argument to a function or as a return value of a function, equivalent to the following form of initialization:

X xx = arg; where xx represents the formal parameter or the return value, and ARG represents the true parameter value.

3. The function is defined as follows: X Bar () {x xx; return Xx;},bar () is copied from the local object XX by a two-step conversion:

ü First add an extra parameter to the bar, which is a reference of class object, which is used to place the return value that was built by the copy.

ü Then place a copy constructor call operation before the return command so that the contents of the object to be passed back as the initial value of the new parameter mentioned above, while overriding the function so that it does not return any value.

4. Named Return Value (NRV) optimization is now considered an obligatory optimization operation of the standard C + + compiler, which is characterized by the direct manipulation of newly added additional parameters. Note that only the appearance of copy constructor activates the NRV optimization of the C + + compiler! NRV optimization, while greatly improving efficiency, has been criticized: first, the optimization is silently completed by the compiler, and its completion is complete and completely transparent; the second is that once the function becomes more complex, the optimization becomes more difficult to implement, and the third is that the optimization may cause the program error Sometimes constructor and destructor are not called symmetrically, but copy constructor is not called!

5. With the compiler providing NRV optimizations, if it is foreseeable that the class requires a large number of memberwise initialization operations, such as passing back objects by value, a explicit inline copy The function entity of constructor is very reasonable. In this case, 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 the program code, especially when a function returns a class object by value, and the class has a copy constructor (or definition or composition). Both the definition of a function and its use will result in esoteric program conversions. In addition, the compiler will implement NRV optimizations.

7. Note that memset () and memcpy () are used correctly, and they are only valid if classes does not contain any internal members, such as VPTR, generated by the compiler!

2002-6-30

2.4 List of member initialization
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, which is within the constructor function itself.

2. In order for the program to compile successfully, the member initialization list must be used in the following situations:

ü Initializing a reference member;

ü Initializes a const member;

ü Call a base class constructor, while it has a set of parameters;

ü Calls a member class constructor, while it has a set of parameters.

3. The compiler will process the initialization list one by one and possibly reorder it to reflect the order of members ' declarations, which will place some code inside constructor and precede any explicit user code.

4. One caveat: Use "a member that exists in constructor" instead of "one member in member initialization List" to set an initial value for another member.

2002-7-1

Data semantics the semantics of data
Discuss the following inheritance system:

Class x{};

Class Y:public virtual x{};

Class Z:public virtual x{};

Class A:public Y, public z{};

1. An empty class such as Class x{}, which has a cryptic 1 byte, which is a char inserted by the compiler, so that two objects of this class can be configured in memory with a unique address.

2. The size of Y and Z is affected by three factors:

U language itself creates an extra burden overhead. The additional burden caused by language support for virtual base classes is reflected in some form of pointer, which either points to the virtual base class Subobject or to a virtual base class A table with an subobject address or offset.

ü The compiler provides optimized processing for special cases. Virtual base class X 1 byte-sized subobject also appear on class Y and Z. Traditionally it was placed at the end of a fixed part of the derived class. Some compilers provide special handling of empty virtual base as part of the derived class object at its very beginning, without any additional space, which is referred to as the 1 byte mentioned earlier.

Üalignment restrictions. Alignment is to adjust the value to an integer multiple of a number, on a 32-bit computer, usually 4 bytes (32-bit), so that the bus traffic to achieve maximum efficiency.

3. A virtual base class Subobject only has an entity in the derived class, no matter how many times it appears in the class inheritance system, the size of Class A is determined by the following points:

ü The only Class X entity that is shared by everyone, the size is 1 byte;

Übase the size of Y and z minus the size configured for Virual Base class;

Üclass a own size;

The number of alignment for Üclass a.

4. C + + standard does not enforce the trivial details of the order of the base class subobjects, data members of different access levels, nor does it specify virtual function and virtual base The implementation details of the classes.

5. The C + + object model is optimized for spatial optimization and access speed to represent nonstatic data members, and to maintain compatibility with C-language struct data configurations. It stores data directly in each class object, and for inherited nonstatic data members, either virtual or nonvirtual base class. The static data members are placed in a global data segment of the program without affecting the size of the individual class object. Static data member always has only one copy of the entity, but a template class's static data member behaves slightly differently.

3.1 Data member Bindings
The data member binding operation within the inline member function body will occur only after the entire class declaration is complete, and the names in the argument list will be resolved by the appropriate resolution when they first encounter them. Based on this situation, always place the nested type declaration at the beginning of class.

2002-7-2

Layout of the 3.2 Data member
1. Each private, protected, and public segment is an Access section. C + + standard requires that, in the same access section, the members ' arrangement only satisfies the condition that the "later members have a higher address in class object". In other words, the members do not have to be contiguous, and the data members that the alignment may need to bytes and the compiler may synthesize for internal use may be between the members that are declared.

2. C + + Standard also allows the compiler to freely arrange data members in multiple access sections without having to care about the order in which they appear in the class declaration. At present, each compiler is a chain of more than one access sections, according to the order of declarations into a contiguous block. The amount of access sections does not cause additional burdens.

3. Vptr is traditionally placed at the end of all clearly declared members, but some compilers now put vptr at the forefront of class object.

4. A template function that is used to determine which section appears first:

Template <class Class_type, class Data_type1, class data_type2>

char* Access_order (data_type1 class_type::* mem1, data_type2 class_type::* mem2)

{

ASSERT (mem1! = mem2);

Return Mem1 < mem2? "Member 1 occurs first": "Member 2 occurs first";

}

(I tested the function under VC + + 6.0, the compilation has not yet passed.) This, should blame the compiler)

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

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.