"Deep Exploration of C + + object Model" Reading notes

Source: Internet
Author: User
Tags bitwise object model

Inside the C + + Object Model

Stanley B. Lippman

Ext: Http://blog.csdn.net/dwater

"Deep Exploration C + + object Model" Reading notes preface Stanley B.LIPPMAN

1. Three conversion flavors required by any object model:

• Transformations that are closely related to compilers

U Language Semantic Conversion

• Conversion of program code and object model

2. Two interpretations of the C + + object model

The part of the language that directly supports object-oriented programming

• The underlying implementation mechanism for various supports

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 the quick dispatch result, pays is the execution period elasticity.

4. All compilers currently implement virtual function using virtual table of each class, fixed in size, and constructed before program execution.

5. The underlying mechanism of the C + + object model is not standardized and varies depending on the implementation of the product (compiler) and the time change.


about Object Lessons 1.1 C + + object mode

1. The main additional burden of C + + in layout and access time is caused by virtual, including the virtual function mechanism and the virtual base class mechanism, and some occur in "a derived class and its second or subsequent base Multiple inheritance on the transformation of class.

2. In the C + + object model, the nonstatic data members are configured within each class object, and the static data members are stored outside of all class object, static and nonstatic The 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 the virtual functions, placed in virtual table ( VTBL) , each class object is added with a pointer vptr, pointing to the associated virtual table. The Type_info object associated with each class is also indicated by VTBL, usually at the first slot of VTBL. Vptr is automatically completed by the Construtor, destructor, and copy assignment operator of each class. The main advantage of the above model is the efficiency of space and access time, 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 are modified.

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

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 augment the original existing VTBL to maintain the position of each virtual base class. 1.2 key words brought about by the difference

1. It can be said that the use of keyword struct with a public interface statement, it can also be said that its purpose is to facilitate the C programmer to migrate to C + + tribe.

2. Data in C + + that is in the same access section is guaranteed to appear in the memory layout in a declarative order, but the order of data placed in multiple access sections is not necessarily the same. Similarly, the layout of the data members of the base classes and derived classes is not the first mandatory rule.

3. Combining composition rather than inheritance is the only way to combine C and C + +. 1.3 Differences in Objects

1. C + + program Design model supports three kinds of program design models programming Paradigms:

• Program Model Procedural models

U abstract data type model abstract, ADT

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

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

3. In C + +, polymorphism exists only in the public class system, the nonpublic derivation behavior and the pointer 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 display transformation operation.

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

U through a set of implicit conversion operations, such as converting a derived class pointer to a pointer to its public base type;

u via virtual mechanism;

U via dynamic_cast and typeid operators.

5. The main purpose of polymorphism is to influence the encapsulation of a type through a common interface, which is usually defined in an abstract base class. This interface is raised by the virtual function mechanism, which can parse the actual type of object to be invoked at the execution time, based on the true types of objects.

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

The sum size of the ünonstatic data members;

• Any space that is filled up by alignment requirements;

• Any additional burdens that are generated internally to support virtual.

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

8. The difference between pointers to different types is not the same as its pointer notation or its content, but rather the type of object it addresses, that is, the pointer type teaches the compiler how to interpret the contents and size of memory in a particular address. They support polymorphism because they do not raise 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 operation is actually a compiler instruction, in most cases it does not change the real address of a pointer, it only affects the size and content that is being directed.

10. When a base class object is directly initialized or specified as a derived object, the derived object is cut sliced to cram into a smaller base type memory, and polymorphism is no longer rendered. A strict compiler can resolve a virtual function invocation operation triggered by the object at compile time, thus avoiding the virtual mechanism. At this point, if the virtual function is defined as inline, it is an efficient gain.

11. C + + through class pointer and reference to support polymorphism, this design style is called oo;c++ also support the specific ADT program style, now known as object-based OB, does not support polymorphism, does not support type expansion.

2002-6-25 structural function semantics 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 operation 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. In various versions of the module, the compiler avoids the process of synthesizing multiple default constructor: The resultant default constructor, copy constructor, assignment copy Operator are done in a inline way. 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.

3. In the following four cases, the compiler must synthesize a implicit nontrivial default constructor for undeclared constructor classes: member class with default constructor Object, base class with default constructor, with virtual function, 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. The compiler synthesized implicit nontrivial default constructor, but secretly did some important things to ensure that the program works correctly and reasonably. 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.

construction operation of 2002-6-26 2.2 Copy constructor

1. In three cases, the content of one class is used as the initial value of another class object:

You explicitly initialize an object, such as: SomeClass obt = OBTB;

U an object is given to a function as an argument, such as: foo (OBT);

U when the function returns a class object.

If the Class Designer explicitly defines a copy constructor, in most cases the constructor is invoked. This can lead to the generation of a temporary class object or the transformation of the program code, or both.

2. If class does not provide a explicit copy constructor, when class object takes another object of the same class as the initial value, its interior is based on the so-called default memberwise Initialization: The value of each built-in or derived data member is copied from one object to another object. However, it does not copy the Member class object, but rather recursively execute memberwise initialization.

3. A class object can be copied in two ways: initialization and designation, conceptually, these two operations are accomplished by copy constructor and copy assignment operator respectively.

4. If class does not declare a copy constructor, there is an implied declaration implicitly declared or an implied definition implicitly defined appears. C + + to the copy constructor divided into trivial and nontrivial two species. Only nontrivial entities are synthesized. The standard for deciding whether a copy constructor is trivial is whether class shows the so-called "bitwise copy semantics".

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

Üclass contains a member object whose class declaration has or is synthesized by the compiler there is a copy constructor;

Üclass inherits from a base class the latter exists or is synthesized by the compiler having a copy constructor;

U when class declares one or more virtual functions;

When class derives from a chain of inherited strings, 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.

6. Once the vptr must be introduced into a class object, 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 initializes an operation with its derived class object content, its vptr copy operation must be secured.

8. Each compiler's commitment to virtual inheritance means that the location of the virtual base class Subobject in derived class object must be properly prepared 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.


data semantics The semantics of data

The following inheritance system is discussed:

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.