C/C ++ Object Model

Source: Internet
Author: User

I. Concept of C/C ++ Object Model

 

The object model mentioned here does not refer to the object model described by Lippman in the book inside the C ++ object model, lippman describes the underlying implementation mechanism of object-oriented, but the object model to be discussed below is a data abstraction framework at the language level. The two are different, object-oriented language facilities are built on the object model. In programming language design theory, there is actually no object concept, only the variable concept, but it is different from the C/C ++ variable. In contrast, it is more similar to the C/C ++ object model.

Similar to the memory model, C only proposes a separate definition of the object, while C ++ puts forward the overall concept. Most of the Basic Semantics of the two are the same. They are all stored in a region, but functions are not objects, that is, objects are data abstraction rather than operation abstraction, and functions are operation abstraction.

 

Ii. Complete object and sub-Object

 

An object can be composed of other objects, including objects called subobject ). The example of a C neutron object is an array element and structure member, while C ++ is more complex than C. In addition to array elements and structure members, it also includes base class sub-objects and member sub-objects. Compared with sub-objects, if an object is not a sub-object of any other object, it is called a complete object. When we say that an object is a complete object, it has two meanings:

1. If this object is a complete object, the complete object of this object is itself;

2. If this object is a sub-object of another object, the complete object of this object is the object that contains it.

For example:

 

Struct a {int I ;};

Struct B: A {Int J ;};

A;

B;

 

B's complete object is B's, but B. j's complete object is not J, but B, and a's complete object is still a. Some people may be surprised that although A and B have an inheritance relationship, however, the division of complete objects and sub-objects is based on instances. Class A objects in B are sub-objects of B, but B is not a complete object of.

 

Iii. Final derived object

 

To distinguish it from base class sub-objects, an object with the most derived class type is called the final derived object (most derived object). What is the final derived class type? The final derived class type refers to the type of the complete object, data member, or array element with the class type, but does not include the derivation relationship of the built-in data type. For example:

 

Struct a {int I ;};

Struct B: A {Int J ;};

Struct c {a k ;};

A;

B;

C;

B d [10];

 

A and B are final derived objects, but Class A sub-objects in B are not final derived objects. d each sub-object is a final derived object, C. K is the final derived object, but. I and. J is not the final derived object.

The binary width of a final derived object cannot be 0. It has at least one or more bytes of storage, and the base class sub-object can only be 0 binary width. For example:

 

Struct {};

Struct B: {};

Struct C: A {a I ;};

A;

B;

C;

 

Although a does not have any data member, because a is the final derived object, sizeof (a) cannot be 0, but a base class sub-objects in B and C can be 0; C. because it is the final derived object, sizeof (C. i) cannot be 0, so that sizeof (c) Cannot be 0.

The concept of final derived object is used to solve the address uniqueness problem arising from empty class instantiation, and the address of the final derived object is always unique.

 

Iv. Object Attributes

 

In addition to the relationships between the complete object, sub-object, and final object, the object also has many attributes, including name, address, value, type, and life cycle.

 

1. Object Name

 

An object has a name attribute, but not all objects have a name. Object declaration associates an object with a name. At this time, the object is also called a variable, and the object name is also called a variable name. Here the description of the variables and variable names is C ++, although the variable term is used everywhere in the context of the C standard, there is no explicit provision on the definition of the variable. This behavior is confusing, and similar examples include entities ).

Anonymous objects are also called anonymous objects. Anonymous objects can be generated by declarations, expressions, or intermediate results. However, anonymous object declarations in C are undefined (except for anonymous fields ), c ++ allows anonymous Joint Declaration. The anonymous object generated by expression calculation is also called a temporary object. Some people call a temporary object as a temporary variable. This is wrong, because the variable is a storage of a name, it must have a name, and there is no unknown variable.

 

2. Object address

 

The object address indicates the location of the object in storage. However, not all objects are addressable. For example, because the minimum object bit field can be smaller than one byte, bitfield cannot perform address fetch operations. For the same reason, the bitfield cannot appear as a complete object, but only as a sub-object.

 

3. object Value

 

The object value is the content contained in the object, but the content of the object cannot always be regarded as a value. For example, clustering and class content cannot be regarded as a value, only the content of an object of the scalar type can be regarded as a value. In C/C ++, the scalar type refers to the numerical type and pointer type.

 

4. Object Storage persistence and life cycle

 

Objects are stored continuously. C/C ++ classifies object storage persistence into three types: static, automatic, and dynamic. Objects with external or internal links or modified by the static keyword have a static storage cycle, and the life cycle starts from the beginning of the program to the end of the program. objects without link and static modification have an automatic storage cycle, the compiler automatically manages the creation and destruction of the object. Objects with dynamic storage cycles are created by malloc (), realloc (), calloc (), or new, and destroyed by the programmer.

The object life cycle is the concept that is most likely to be confused with the object storage continuity. The object Life Cycle refers to the duration of the process from object creation to object destruction, although the storage persistence largely determines the object survival mode, it is essentially the object storage management method.

 

5. Object Type

 

Object type is the most important attribute of an object. It determines the object type and size. In general sense, it seems that the object type belongs to the object property. For C ++, C ++ believes that the object type is inherent and exists at the moment of object creation, however, the object value can be interpreted by the type of the expression that accesses the object, that is, the value of the same object can be represented by different values. However, c's concept is the opposite of the above situation. c thinks that the type is not the attribute of the object, and there is no type information either in the object itself or internally. The C standard points out in rational:

 

3. Terms and Definitions

 

The definitionObjectDoes not employ the notion of type. Thus an object has no type in and of itself. However, since an object may only be designated by
Lvalue(See § 6. 3.2.1), the phrase "the type of an object" is taken to mean, here and in the standard, "the type of the lvalue designating this object, "and" the value of an object "means" the contents of the object interpreted as a value of the type
Of the lvalue designating the object ."

 

In C, the object type is determined by the expression that references the left value of the object, rather than the object itself. When we say that an object has a specific type, the type of the expression that references the left value of the object.

The huge difference in object types leads to different interpretations of object values in C and C ++. See the following code:

 

Int I = 0x01010101;/* assume that Int Is 32 characters */

Short * P = (short *) & I;/* assume that short is 16 bits */

 

In C, * P and I are considered to reference two different types of objects with different values. For C ++, * P and I reference the same object, but from the perspective of * P and I, the value is different. Although the underlying interpretation is different, the effects are the same from the perspective of the expression that references the object.

Another question is, how can I access the object content ?, There are some differences between C90, c99, and C ++.

In C90, because the left value must indicate a valid object (which is actually a mistake), C90 requires that the value of an object be accessed only through its declared type (Declaration type) this rule denies the pointer P access method of the above Code (the problem caused by the above error), but the pointer P access method actually exists and is feasible, and it is obviously unreasonable to deny it.

Therefore, this error in c99 is corrected. c99 no longer specifies that the Left value must reference a valid object, and introduces the concept of valid type, c99 divides the valid type into four cases, but simply put, the valid type of an object with the declared type is the declared type, the valid type of an object without a declared type is the type of the left-value expression for this access. The latter description ensures the validity of the above-mentioned p access method. C99 operates on Object content through valid types.

In C ++, because OOP is introduced, class objects inherited from hierarchies have polymorphism, and their types are called dynamic types, which are determined by the runtime, therefore, C ++ accesses objects through dynamic types.

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.