"Deep Exploration of C + + object Model" Reading notes--chapter __c++

Source: Internet
Author: User
Tags object model advantage

The Deep Exploration of C + + object model
Reading notes
Preface: Know it, know it
1, this book is written by senior C + + programmers;
2, this book focuses on: various C + + object model to support the underlying implementation mechanism;
3, the programmer should understand the underlying implementation model, in order to become a master.
Lippman:
1. Three conversion flavors required by any object model:
(1) Transformations that are closely related to the compiler
(2) Language semantic conversion
(3) Conversion of program code and object model
2. Two interpretations of the C + + object model
(1) The part of language that directly supports object-oriented programming
(2) for various support of the underlying implementation mechanism
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.
Chapter One about objects
The difficulty of this chapter:
1. Table-Driven object model
2. The additional burden created by virtual
Knowledge Points:
1.0 Introduction
The main additional burden of C + + in layout and access time is caused by virtual, including:
(1) virtual function
(2) Virtual base class (dummy inheritance)
There are also multiple inheritances that occur on the "one derived class and its second or subsequent conversion to base class."
1.1 C + + object model
1, in the C + + object model,
(1) The nonstatic data members are configured within each class object,
(2) The static data members are stored outside all class object,
The static and nonstatic function members are also placed outside of all class object,
(3) Virtual functions is supported by two steps:
1 Each class produces a bunch of pointers to virtual functions, placed in virtual table (VTBL);
2 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.

First, Simple object model:
A pointer to a member is held within the object, and no member is stored.
• Sacrificing space and run-time efficiency. Resolves storage space inconsistencies caused by inconsistent member types.

Second, table-driven object model
Table-Driven object model:
 Need two tables, data member table and member function table, data member table directly store the data itself, member function table to store the address of each function.
object holds the pointer to both tables.
 The idea of the member function table can support virtual function implementation.

Three, C + + object model
(1) Stroustrup design, derived from the Simple object model.
(2) Non-static data members are stored within each class object, and static data members are stored outside of the individual class object.
(3) The static and Non-static member functions are also stored outside of the individual class object.
(4) Virtual functions are implemented through virtual function table VTBL and pointer vptr to virtual function table:
(5) Each class object has a vptr that points to the associated VTBL.
(6) The setting of vptr is accomplished by the construction/destructor/copy function of the class.
(7) The first item in the VTBL table is the Type_info object associated with each class (used to support run-time type recognition Rtti), and each of the other items holds the address to the virtual function.

Iv. Multi-Inheritance

Class Istream:virtual public ios{};
Class Ostream:virtual public ios{}; Virtual inheritance



1.2 Key words brought about by the difference
1. The use of keyword struct is accompanied by a declaration of a public interface, which can be said to be used only to facilitate C programmers migrating to C + + tribes.
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:
(1) Program Model procedural models
(2) abstract data type model abstract, ADT
(3) 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:
(1) through a set of implied conversion operations, such as converting a derived class pointer to a pointer to its public base type;
(2) through the virtual mechanism;
(3) 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 resolves which function entity is invoked at the execution time according to the true type of object.
6. The memory required by a class object, consisting of the following parts:
The sum size of the nonstatic data members;
 Any space that is filled up as a result of alignment demand;
 Any additional burden that arises from the interior in support of 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 + + supports polymorphism through class pointer and reference, which is called Oo, and also supports specific ADT program styles, that is, object-based OB, which does not support polymorphism, and does not support type expansion.
Polymorphism can only be implemented by pointers or references (not through instance objects), because:
 The size of pointers and references (which are usually implemented as pointers) is fixed (a word), while objects are of variable size. Pointers and references to their classes can point to (or refer to) subclasses, but objects of the base class can never be base classes, and no changes can cause polymorphism.
 A pointer or reference will never raise any "type-related memory delegate operations", and will be changed only by the way they point to memory when the pointer type is converted. (for example, pointers do not raise slice of space because they are the same size)

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.