Deep Exploration of C + + object model (Inside the C + +) Learning Notes

Source: Internet
Author: User
Tags bitwise numeric value shallow copy

Source: http://dsqiu.iteye.com/blog/1669614


has been completely blank to the principles of C + + internal, then find "Inside the C + + Object Model" This book looked, feel a great harvest, because the writing is relatively early, some knowledge should be updated, but still worth studying, because the content of the book to give people a more scattered feeling, So always want to find a time to tidy up, hence into this article, although all is transcription on, but let me have the temperature so know the new consciousness, nearby have three good data, share together. September 2, 2012 4:31 Thank you for Backstreet Boys's singing companion.

Deep Exploration of C + + object model (Inside the C + +) Learning Notes

Chapter One about objects

Layout costs after using class encapsulation:

Class does not add cost, the data members are directly embedded in each class object, just like C struct. The member functions, though contained within the class declaration, does not appear in object. Every non-inline function produces only one function entity. As for the inline function, a function entity (which expands the function body at the call point) is generated where each call is used.

The main additional burden of class in layout and access time is caused by virtual, including:

The virtual function mechanism is used to support an efficient "execution binding (runtime Binding)".

Virtual base class is used to implement the base class, which appears multiple times in the inheritance system, with a single shared entity.

There are, of course, some additional burdens that are chopped into inheritance, which occur between "a derived class and its second or subsequent conversion to the base class."

C + + object model

In the C + + object model, the nonstatic data members are placed within each class object, and the static data members are stored outside of the class object. The static and NONSTAITC function is also placed outside all class object. Virtual functions is supported in two steps:

1. Each class produces a bunch of pointers to the virtual functions, placed in the table, which is called virtual table (VTBL).

2. Each class object is added with a pointer to the associated virtual table. Usually this pointer is called VPTR, and vptr settings and resets are automatically completed by the constructor, destructor, and copy assignment operators of each class.

C + + supports polymorphism in the following ways:

1. Through a set of implied conversion operations. For example, a derived class pointer is converted to a pointer to its public base type.

2. Through the virtual function mechanism.

3. Via dynamic_cast and typied operators.

How much memory is required for class object:

1. The total size of its nonstatic data members.

2. Add any hesitation alignment needs and padding (fill) up the space.

3. Add any additional burdens that arise from the interior in order to support virtual.

Pointer type: Tells the compiler how to interpret the memory content in a particular address and its size (for example, a string is a traditional 8 bytes (including a 4byte character pointer and an integer that represents the length of the string). Transformation (CAST) is actually a compilation of instructions, most of which will not change the real address of a pointer, it only affects the "indicated value of memory size and its content" interpretation.

Chapter II Semantics of structural functions

2.1 Default Constructor

When the compiler needs it, the default constructor is synthesized, executing only the tasks required by the compiler (properly initializing the members).

Member Class Object with Default constructor

The compiler comes out: If a Class A contains one or more member class objects, then each constructor of class A must invoke the default constructor of each member classes 。 The compiler expands the existing constructors and puts some code in it, allowing user code to invoke the necessary default constructors before it is executed (call order one member objects in the class declaration order).

Base class with Default constructor

The compiler invokes the default constructor of all base class constructor (invocation order according to their declaration order) before the default constructor of member Class Object is inserted into the call.

Class with a Virtual Function

The following two conditions also require the synthesis of default constructor:

1.class declares (or inherits) a virtual function.

2.class derives from a chain of inherited strings, one or more virtual base class.

The extension (constructor) operation occurs during compilation:

1. A virtual function table is generated by the compiler, with the address of the class's virtual functions.

2. In each class object, an additional pointer member (VPTR) is synthesized by the compiler, containing the address of the associated class VTBL.

Class with a Virtual Base class

The implementation of virtual base class differs greatly between compilers, however, the common denominator of each implementation is that the virtual base class must be positioned in each of its derived class object to be ready during the execution period. For each constructor compiler defined by class, the code that "allows the execution of each virtual base class to be accessed" is placed.

Summarize

The above four scenarios cause the compiler to synthesize a default constructor for classes that are not declared constructor, which is the need for compilers rather than programs. It is able to complete the task by "invoking the default constructor of the member object or base class" or "initializing its virtual function mechanism or virtual base class for each object Mechanism "completed. Classes that do not exist in these four situations and have no life constructor are not actually synthesized.

In the synthesized default constructor, only the base class subobjects (child objects) and member class objects are initialized. All other nonstatic data member, such as integers, integer pointers, integer arrays, and so on, are not initialized, and these initialization operations are necessary for the program, but not for the compiler.

C + + Novice generally has two misunderstandings:

1. Any class that does not define default constructor will be synthesized.

2. The default constructor created by the compiler explicitly sets the defaults for each data member within the class.

2.2 Copy Constructor

In three cases, an object is used as the initial value of another class object.

1. The most obvious of course is the explicit initialization of an object.

2. When object is given to a function as an argument

3. When the function returns a class object.

Copy constructor are required in these three situations.

Default memberwise Initialization

If class object takes the "same another object as the initial value, the internal is in the so-called default memberwise if the classes do not provide a explicit copy constructor Initialization the way it was done. That is, the value of each built-in or derived data member (such as an array or pointer) is copied from an object to another object, but does not copy its specific contents. For example, just copy the pointer address, not a copy of a new pointer to the object, which is a shallow copy, but it does not copy the Member class object, but in a recursive way to implement memberwise initialization.

How does this recursive memberwise initialization be achieved?

The answer is bitwise copy semantics and default copy constructor. If class shows bitwise copy semantics, then using bitwise copy (the bitwise copy semantics compiler generates pseudo code is a memcpy function), the compiler generates default copy constructor.

So under what circumstances class does not show bitwise Copy semantics. There are four kinds of situations:

1. When class contains a member class object, and the member class has a default copy constructor [whether the class designer explicitly declares or is synthesized by the compiler]

2. When class inherits from a base class, and base class has the copy constructor [whether the class designer explicitly declares or is synthesized by the compiler]

3. When a class declares one or more virtual functions

4. When class derives from a chain of inherited strings, one or more virtual base class

Let's understand why these four scenarios do not use bitwise copy, and what the compiler-generated copy constructor does.

In the first 2 cases, the compiler must place the "copy constructor invocation operation" of member or base class into the synthesized copy constructor.

Reset a pointer to virtual Table

In the 3rd case, because class contains virtual function, the build requires an expansion operation:

1. Add the virtual function table, which contains the address of a functioning virtual function;

2. Create a pointer to the virtual function table, placed in class object.

Therefore, the compiler for each new class object vptr must be correctly assigned, otherwise it will run to execute the function of other objects, the consequences are very serious. Therefore, when the compiler imports a vptr into class, the class does not show bitwise semantics, the copy constructor must be synthesized and vptr properly initialized.

Handling Virtual Base Class subobject

The existence of virtual base class requires special handling. A class object can invalidate "bitwise copy Semantics" If it is subobject with another virtual base class.

Each compiler's support commitment to virtual inheritance means that the "virtual base class Subobject location in derived class object" must be ready during the execution period, and that maintaining "location integrity" is the responsibility of the compiler. Bitwise copy semantics may destroy this location, so the compiler must synthesize the copy constructor itself.

This means that the copy constructor, like the default constructor, is built when needed, rather than being built by the programmer without writing a compiler.

2.4 Initialization List

The following four cases must initialize the members of class using the initialization list:

1. When initialization of a reference member;

2. When a const member is initialized;

3. When the constructor of a base class is invoked and it has a set of parameters (in fact, a custom constructor);

4. When you invoke the constructor of a member class, and it has a set of parameters.

However, the order of initialization is determined by the class Members declaration order, not by the initialization list.

Chapter III Data Semantics studies

Layout of 3.2 Data member

The order in which the nonstatic data members are arranged in class object will be the same as the order in which they are declared. But C + + standard allows the compiler to freely arrange the data members in multiple access sections, regardless of their order of declaration appearing in class.

Access to 3.3 Data member

Each member's access License (private public protected), and its association with class, does not cause any additional burden on the space or execution time-whether in the individual class objects or in the static data Member itself.

The static data members are treated as global variables, and only one entity is stored in the program's data segment, and each static member is internally transformed into a direct reference to that unique extern entity. If you take the address of a static data member, you get a pointer to a datatype, not just a pointer to class member.

nonstatic data members want to access a nonstatic data member, the compiler needs the starting address of class object plus the offset of data member (which can be learned at compile-time).

3.4 Inheritance with data member

As long as inheritance does not polymorphic

This situation does not increase the extra burden on space or storage time. In this case, both the base class and the derived class objects start with the same address, and the difference is that the derived object is larger to accommodate the nonstatic data members of the build, and a derived Class object is assigned to a pointer or reference to base class and does not require the compiler to mediate or modify the address, it is moist and can occur, and provides optimal execution efficiency.

Plus polymorphism

This creates an additional burden of space and access time:

1. Import A and virtual table to store the address of each virtual functions that it declares.

2. Import a vptr in each class object, providing a link to the execution period so that each object can find the corresponding virtual table.

3. Strengthen the constructor so that it can set the initial value for the vptr, so that it points to the corresponding virtual table class.

4. Strengthen the destructor, so that it can erase the "Point class related Virtual table" vptr.

Multiple inheritance

For a multiple derived object, the assigning its address to the "left (first) base class pointer" is the same as for a single inheritance, since both point to the same starting address, and the address of the second or subsequent base class specifies an action that needs to be modified: plus (or minus , if downcast) is the size of the base class Subobject (s) in the middle.

If you want to access a data member in the second (or later) base class, there is no extra cost because the members ' location is fixed at compile time, so accessing member is just a simple offset operation.

Virtual inheritance

Class if it contains one or more virtual base class subobjects will be divided into two parts: a invariant local and a shared part. Invariant local data, always have fixed offset, this part can be directly accessed, as for the shared part, the performance is virtual base class Subobject, this part of the data, its location because each derivation of the change, so only indirect access.

Without virtual functions, they are exactly the same as C struct.

Fourth Chapter Function Semantics study

Various ways to invoke 4.1 member

nonstatic member functions

In fact, the compiler is the form of the member function being internalized into nonmember, through the following transformation steps:

1. Add additional parameters to the function--this.

2. The access operation for each NONSTAITC data member will be changed to this pointer for access.

3. Rewrite the member function to an external function. Select mangling for the function name to make it a unique vocabulary.

Virtual member functions

Will

Ptr->f (); F () is virtual member function

Internally translated

(*ptr->vptr[1] (PTR);

which

Vptr represents the compiler-generated pointer, pointing to virtual table. It is placed in every class object that declares (or inherits from) one or more virtual functions.

1 is the index value of virtual table slot, which is associated to the normalize () function.

The second PTR represents the this pointer.

Static member functions

cannot be declared as const volatile or virtual.

A static member function is presented outside the class declaration and given a mangling appropriate name. If you take the address of a static member function, you get its location in memory, which is the address, not a pointer to the class member function, as follows:

&point::count ();

Will get a numeric value, the type is:

unsigned int (*) ();

Instead of:

unsigned int (point::*) ();

4.2 Virtual member Funcitons

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.