Inside C ++ object model-Object Model Overview

Source: Internet
Author: User

In C, "data" and "data processing operations" are declared separately. The language itself does not support the association between "data and functions. this is called "procedural" and is driven by a group of Algorithm functions. They process common external data.

C ++ has obvious differences in program style and thinking. It uses ADT or class hierarchy Data encapsulation to establish correlation between data and operations. it is more reasonable from the perspective of software engineering.

But programmers often choose C.

 

Then, does C ++ mean inefficiency...

In fact, the extra burden of C ++ in layout and storage time is mainly caused by "virtual,

  • TheVirtual FunctionMechanisms in its support of an efficient run-time binding, and

  • AVirtual base classIn its support of a single, shared instance of a base class occurring multiple times within an inheritance hierarchy.

Generally, there is no natural reason to say that C ++ must be larger or slower than C.

 

Object Model

As the basis for the discussion in this book, we should first analyze the object model of C ++. The so-called object model is how to store an object in memory.

The following three models are available for Object Storage:

Simple Object Model

This model, as its name implies, is developed to minimize the design complexity of the C ++ compiler. Of course, it is simple to bring about inefficiency in space and execution period.

In this model, the object is a series of slots. The slots contains pointers to members, which are arranged in the declared order of the program.

The object does not actually store members, but only the pointer of member. The advantage is that the type of member does not affect the storage space of the object.

However, the access efficiency and storage efficiency are reduced, because the access needs to be indirectly accessed through pointers, and additional pointers need to be stored.

 

Table-Driven Object Model

This model is more abstract than a simple model. It places data member and function member in two tables respectively,

The member function table stores function pointers like the simple model. The data member table directly stores data itself.

The object itself only stores the pointers of these two tables. The biggest advantage is that the object size is fixed, no matter how members changes.

 

C ++ Object Model

The C ++ model is derived from the simple object model and optimizes the memory space and access time. The storage methods of each member are as follows,

Nonstatic data membersAre allocated directly within each class object.

Static data membersAre stored outside the individual class object.

Static and nonstatic function membersAre also hoisted outside the class object.

Virtual FunctionsAre supported in two steps:

  1. A table of pointers to virtual functions is generated for each class (this is calledVirtual table).

  2. A single pointer to the associated virtual table is inserted within each class Object (traditionally, this has been calledVptr).Type_infoObjectAssociated with each class in support of runtime type identification (rtti) is also addressed within the virtual table, usually within the table's first slot.

As shown in, for the c ++ object model, only nonstatic data members and vptr are saved in the object. The advantage of this model is the efficiency of space and access time, the disadvantage is that, considering the access efficiency, nonstatic data members is directly stored in objects, so any changes to data members will affect the size of class objects. here, the table-Driven Object Model provides greater flexibility, but the fish and the bear's paw cannot have both. during the design, we need to make trade-offs based on actual needs. c ++ focuses more on efficiency, while a component platform such as CORBA will choose a table-driven model with lower coupling, so that when the class members change, you do not need to re-compile the Customer Code (because the object size remains unchanged for this model ).

Inherited Object Model

We have discussed the basic C ++ object model. If the inherited language feature is added, what changes should be made to the object model?

This problem is how to store parent classes, derived classes, and relationships between them in the memory.

The simple idea is to add a slot to the object to store the parent class Object Pointer, and add several slots for several parent classes.

A more transparent method is to generate a base class table to store all parent class pointers, and add a bptr to the object to point to the table.

The problem with this method is the efficiency problem caused by the indirect nature, but the advantage is that the object can modify the parent class as needed for consistent inheritance, without affecting the class object itself.

 

As mentioned above, C ++ focuses more on efficiency, so it does not adopt any indirect method,

The data members of the base class subobject are directly stored within the derived class object.

The disadvantage is obvious, that is, any base class is changed, and all the derived classes that use this class need to be re-compiled.

The processing of the virtual base class in C ++ will be discussed in the following chapter...

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.