Deep exploration C ++ object model first chapter about objects

zhaozj2021-02-16  55

In the C language, "data" and "processing of data" are very open. Driven by a set of "distributed in various one-piece taste" algorithm that handles common external data. In C , the independent "Abstract Data Type Abstract Data Type (ADT)" is used. From the perspective of software engineering "A ADT or Class Hierar5chy data package" than "use global data in the C program". " Plus the packaged layout C in the layout and the additional burden on the access time is caused by Virtual, including: Virtual Function mechanism to support an efficient performance binding "Runtime Bingding" Virtual Base Class is used to implement " Baseclass in the inheritance system has appeared many times, there is a single-shared entity "

1.1 C Object Model (THE C Object Model) In C , there are two Class Data Members: static and nonstatic; three Class Member function: static, nonstatic and virtual. The arrangements for these contents include: 1 Simple object model to minimize the design complexity of the C compiler, equipped with the efficiency of space and execution period. An Object is a series of Slots, every SLOT thinks a MEMBER: Data MEMBER AND FUNCTION SLOT. Each Data MEMBER AND FUNCTION MEMBER has its own slot. In this model, Meember is not placed in Object, but stores SLOT Put Data Member in a table by Slot point to the MEMBER.2 table Drive Object Model This model is placed in a table. Split Data Member and MEMBER FUNCTION. 3 C Object Model NonStatic Data Members The North is designed to be in Class Object, and Static Data Members is placed outside of Class Object, and Static and NonStatic Function Memeces is placed outside of all Class Object. And the Virtual Functions is completed: 1) Each Class creates a pair of Virtual functions pointers, placed in Virtual Table 2) Each Class Object adds a pointer to point to the related Virtual Table. And VPTR The fixed reset is done by the Constructor, Destructor, and CONPY CONSTRUCTOR. The type_infor associated with each Class index is also done by the Virtual Table, stored in the first slot in the table. Advantages: The efficiency of space and access time; Disadvantages: If the application code itself has not changed, the use of Class Objects NonStatic Data Members is modified, then which application code needs to be recompiled.

Plus inheritance: 1) Simple model: Each base class can be pointed out by a slot in Derived Class Object, which is the address of the Slot Class SubObject. Disadvantages: Indirectivity leads to the burden on space and access time, advantage: The size of Class Object is not affected by Base Class. 2) Base Table model. Base class table generated, each SLOT in the table contains a related base class address, and I want to virtual Table in connotation of a Virtuabl Funciton address. Each object contains a BPTR, pointing to Base Class Table. Disadvantages: Due to indirectness results in additional burden advantages: Each Class Object has a long way to express: Each Class Object should A base Table pointer is placed in a fixed location, independent of the size and trees of Base Classes, and does not change the size of Class Objects. 3) C model: Base Class Subject Data Member Data MEMBERS is placed directly in Derived Class Object, improves access efficiency, but the TV Each base class data member changed, Derived Class changed the need to recompile. The generation of Virtual Base Class requires an indirect Base Class's performance method. See how the object model affects the program: Different object models can cause existing program code to be modified and the new program code must be added.

1.2 The differences brought about by keywords in order to support eight integers, C supports operator overload. In C , data in the same access section must be guaranteed, in the order in which it declares in the memory layout, and the storage order of different Access Sections is uncertain. Similarly, the layout order of Base Class and Derived Class is not specified.

1.3 Triction of objects C supported three programming paradigms: 1) Program Model: Similar to C Language 2) Abstract Data Type Model: A set of abstract data and its corresponding expression 3) Object-oriented model: Type related to each other, Packed by an abstract base class. C one-click method supports polymorphism: 1) Give the address of Derived Class Object to Base Class PoInter: Shapre * ps = new circle (); PS-> Ritate (); 3) via Dynamic_Case and TypeID operators: IF (Circle * PC = Dynamic_Case (PS)) ...

How much memory needs to be expressed in Class Object: 1) Nonstatic Data MEMBERS Size 2) ALIGNMENT Demand 3) Extraffect of Virtual Funciton and Virtual Base Class

Type of pointer: The size of each pointer is the same, but there is a difference between "pointing each pointer to different types", each pointer can only cover the memory range of the object.

转载请注明原文地址:https://www.9cbs.com/read-24324.html

New Post(0)