In-depth exploration of the object of the C ++ object model (Object Lessons)

xiaoxiao2021-03-06  61

December 4, 2004 11:34:42

It is well known that C is a multi-field language. Includes: Process, Abstract Data Type (ADT, Object-Based), object-oriented (Object-Oriented). The so-called process-friendly design method is the design method of traditional C processes: data and processing of data are declared separately. Object-based programming methods package data and operations (such as String Class), he does not support classes, and there is no polymorphism. But the OB design is faster than a peer OO design and is more compact. Object-oriented programming believes that all things are objects, support inheritance, packaging and polymorphism. The relationship between objects is the key points and difficulties of design, to be low coupling, easy to expand, easy to maintain. Finally C also supports generic program, the type can be parameterized, and the number can also be parameterized, and this is the highest realm and development prospects of C .

The cost of memory layout when C is not significantly increased. The memory required by a Class Object includes the following three parts: 1. The sum size of NonStatic Data MEMBERS; 2. Plus any space that is filld from the need for Alignment (boundary adjustment); 3. Plus in order to support Virtual Any additional burden generated. The main additional burden of C in the layout and time of access is Virtual, including: 1.Virtual function mechanism - to support an efficient "Performing Bind". 2.Virtual base class - To achieve "Base Class in the inheritance system, there is a single-shared entity". (Virtual is sharing means)

C Object Model: In this model, NonStatic Data Members is configured within a Class Object, and Static Data Members is stored in all Class Object, Static and NonStatic Function Members are also placed in all Class Object. outer. Virtual Functions supports two steps:

1. Each Class produces a pile of pointers pointing to Virtual Functions, placed in the form, which is called Virtual Table (VTBL).

2. Each Class Object has been added a pointer to point to the associated Virtual Table. Usually this pointer is called VPTR. VPTR settings and resets are automatically completed by each Class's Constructor, Destructor, and Copy Assignment operators. The Type_info Object associated with each Class is also pointed out via the Virtual Table, which is usually the first SLOT in the table.

Plus inheritance: C supports single inheritance and multiple inheritance. Inheritance relationship can also be specified as virtual, in the case of virtual inheritance, Base Class does not have to derive it in the inheritance string, there will be only one entity. How do Derived Class plastics its base class entity in nature? Generally, a Base Table model similar to Virtual Table is generally generated, and each slot in the table contains a related base class address in each slot in the table. Each Class Object contains a BPTR, he will be initialized, Point to its base clas table. The main disadvantage of this strategy is due to the additional burden of space and access time due to indirectity. The advantage is that in each Class Object, there is a consistent manifestation: each class object should be in a certain A base table pointer is placed in a fixed position and has nothing to do with the size and number of base classes. The second advantage is that it is not necessary to change the Class Objects itself, it can zoom in, narrow, or change the base class table. Ke Keywords in C Struct is just to make C programmers and existing C processes easier to C . In addition, Struct and Class have no essential difference in C . You can use the Struct with Class. C struct in C is a reasonable use of C is that when you want to pass all or some of the complex Class Object to a C function, the struct declaration can encapsulate data, and ensure that you have compatible with C. Spatial layout. However, this guarantee is only in the case of a combination.

C the following method supports polymorphism:

1. Via a set of implicit conversion operations. For example, transforming a Derived Class Pointer to a pointer to its public base type: shape * ps = new circle (); 2. via the Virtual Function mechanism: ps-> rotate (); 3. Via Dynamic_CAST and TYPEID operators: IF (Circle * PC = Dynamic_cast (ps)) ...

The main purpose of polymorphism is to affect the type of package via a common interface, which is usually defined in an abstract base class. This shared interface is triggered by the Virtual Function mechanism. C supports polymorphism via Pointer or Reference: BEAR B; Zooanimal * PZ = & B; Bear * Pb = & B; In addition to Members that appears in ZooanImal SubObject, you cannot use PZ to directly process any member of BEARs. // Error, Cell_Block is not a member of Zooanimal, although we know that PZ is currently pointing to a Bear Object. PZ-> Cell_Block; unique exception is through the Virtual mechanism: pz-> rotata (); PZ type will determine the following two points in the compile period: 1. Secure available interface. That is, PZ can only call Zooanimal's public interface; 2. Access Level of the interface. At each execution point, the Object type referred to in PZ can determine the entity called by Rotate (). The package of type information is maintained between "Object's VPTR" and "VIRTUAL TABLE" referred to in VPTR. Take a look at this situation: Bear B; Zooanimal ZA = B; // will cause cut (SLICED ZA.ROTATE (); // Call Zooanimal :: rotate () "more than one The potential power of types is not capable of actually playing things in "direct access to Objects". A Pointer or Reference supports polymorphism because they do not trigger any "memory delegate operations related to the type" in memory; the "size and content interpretation of the memory" will be changed. "

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

New Post(0)