Deep understanding of C + + object model _c language

Source: Internet
Author: User
Tags inheritance object model

What is the C + + object model?

The C + + object model can be summed up in the following 2 parts:

1. Parts of language that directly support object-oriented programming

2. Underlying implementation mechanisms for various supports

Introduction

Now there is a point class, declared as follows:

Class Point {public
 : Point
 (float xval);
 Virtual ~point ();

 float x () const;
 static int Pointcount ();

 Protected:
 virtual ostream& print (ostream &os) const;

 float _x;
 static int _point_count;
};

What model does this class represent on a machine? Here are three different implementations.

1. Simple Object Model

The Simple object model is truly simple. In the Simple object model, one is made up of a object series of slots components, each equivalent to a slot pointer, pointing to a, in the member memebers Order of declaration, slots corresponding to one by one, here's the members package 括data members and function members .

If you apply a simple object model to point class, the structure diagram is as follows:

Advantages: very simple, reduce the complexity of compiler design.

Disadvantages: reduced efficiency in space and time. Because all member of them correspond slot to one pointer, each object extra space is member's number multiplied by the size of the pointer. Also, because object each access member requires slot an extra index at once, the efficiency of the time is reduced.

2. Table-Driven object model

The table-driven object model maps to and member data member function respectively two tables member data table function member table , and object stores only pointers to these two tables. Which function member table is composed of a series of slot, each slot pointing to one member function ; member data table is directly stored in member data itself. If you apply the table-driven object model on, the chart is Point Class as follows:

Advantages: The use of two-tier indexing mechanism to object provide a better flexibility of change object , nonstatic data member when the change in the application code has not changed, at this time do not need to recompile.

disadvantage: The efficiency of space and time is reduced, the concrete reason can refer to the shortcoming analysis of simple object model.

3. C + + object model

Stroustrup The early-designed C + + object model was improved from the simple object model, and the memory space and access time were optimized. The main is to be nonstatic data members stored in each one object , and static data members all of them function members are stored independently object .

Support for virtual functions is accomplished mainly through the following points:

All contains a virtual function or inherits the base class of its own virtual function class virtual table , and the virtual function table stores a bunch of pointers to the virtual functions contained in the class.

Each class associated type_info object is also virtual table stored, and typically the first of the table is slot type_info used for support runtime type identification (RTTI).

If you apply the C + + object model to point class, the structure diagram is as follows:

Advantages: Space and access efficiency, all static data members and all of which function members are stored independently of all object, can reduce each object size, and nonstatic data members stored in each object , and increase the efficiency of access.

Disadvantage: If the application's code has not changed, but the use class of nonstatic data members changes, then the code will still need to recompile all, and the previous table-driven model provides a greater flexibility in this respect, because he provides more than a layer of indirection, Of course, it pays the price of time and space.

Object model in addition to inheritance

C + + supports single inheritance, multiple inheritance, and virtual inheritance, and here base class derived class 's how the entity is built in.

An address that can be stored through one of the simple object models derived class object slot so that base class subobject members can be slot accessed through that base class . The main disadvantage of this implementation is that there is an additional burden on space and access time because of the indirect storage; The advantage is that the derived class structure will not base class change because of the change.

A table-driven object model can use a similar base class table table to store information for all base classes. The table stores a series slot slot of addresses, each of which stores one base class . The disadvantage of this implementation is that there is an additional burden on space and access time because of the indirect storage; The advantage is that all inherited class consistent representations (including a base table pointer to the base class table) are not related to the size and number of the base class, and the second is base class table Increases the extensibility of subclasses, which can be adjusted by scaling, narrowing, or changing when the base class changes base class table .

An important problem in both implementations is the extra burden on space and time caused by indirection, and the progression of the indirection increases with the depth of inheritance.

The inherited model used in C + + does not use any indirection, and all base class data is stored directly in subclasses, which is the most efficient storage structure and access efficiency. Of course, there are drawbacks: when base class members there is any change, the base class object used or derived class must be recompiled. Introduced in C + + 2.0 virtual base class , a number of indirect ways are needed to support this feature, and typically import one virtual base class table or expand existing ones virtual table .

Summarize

The above is the in-depth study of C + + object model of the entire content, I hope the content of this article for everyone to help.

Related Article

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.