In-depth exploration of the C ++ Object Model (I)-object and Object Model exploration

Source: Internet
Author: User

In-depth exploration of the C ++ Object Model (I)-object and Object Model exploration

Introduction

I used to read "C ++ Primer" with the feeling that although the book is a C ++ entry-level book, it is difficult for beginners to read it. The more I read it later, the more I read it. After a little understanding, I will read the book again. The reason is that the original author Stanley Lippman always intentionally or unintentionally introduced the details of the language from the perspective of the compiler: where will the novice focus on such underlying implementation?

When I read "Inside The C ++ Object Model", The above feelings become more and more intense. The details gradually described between The two books are fascinating after reading them, and I am deeply impressed by The wisdom of The masters.

As we all know, C ++ is a language supporting multi-Paradigm ("Objective C ++" Item1). The most important of them is the object-oriented design idea. Based on its introduction, this book explores program behavior under "C ++ object model supported by object-oriented programs. Provide a clear understanding of "basic implementation technology of object-oriented nature" and "Implicit interest exchange behind various properties. Test the efficiency impact caused by program deformation. Measure the efficiency between the object-oriented concept and the underlying object model.

About Objects

When converting from C to C ++, the obvious difference lies in the transition from global data to data encapsulation. What is the Layout Costs? The answer is no increase. This article will be described laterThe main extra burden of C ++ in layout and access time is caused by virtual, Including:

  • The virtual function mechanism is used to support efficient "binding during execution ".
  • Virtual base class mechanism to implement subobject sharing the virtual base class.

In addition, there are some extra burdens of Multi-inheritance. In addition, C ++ has no reason to be slower than C.

C ++ Object Mode

Class Members include class data members (static and non-static) and class member functions (static, non-static, and virtual functions ).

Consider the following simple object model:

We can see that Members are not in objects and only pointer to objects. This can avoid different storage spaces caused by different types of members. Obviously, members are addressed by the index value of each slot.

Consider the table-Driven Object Model (see Figure 1.2 above ):

In this solution, all members are separated into two tables: data member table and member function table. The class object contains pointers to the two tables.

Unfortunately, none of the above two solutions are actually applied to the C ++ compiler, but their design ideas are more or less inherited.

Let's look at the implementation of the C ++ object model (see Figure 1.3 on the top right ):

In this model, non-static data members are placed in class objects, static data members are placed outside class objects, and static and non-static function members are also placed outside class objects. The following steps are supported by virtual functions:

  • Use a virtual function table (VTBL) to record the pointer to the virtual function;
  • The class object points to the virtual function table with a pointer (VPTR), and The vptr operation is completed by the replication control of the class.
  • The type_info object associated with the class is pointed out by the virtual function table and is located at the first slot.

The advantage of this model is the efficiency of space and access time. The main disadvantage is that non-static data members must be re-compiled during modification.

Add inheritance

In the case of virtual inheritance, the base class has only one entity (subobject) no matter how many times it is derived ). C ++'s original inheritance model does not apply any indirect property: the data members of base-class entities are directly placed in the derived class objects. Later, the virtual base class was introduced, and some indirect base class presentation methods were used. The specific implementation will not be elaborated in this article, and will be reserved for subsequent blog posts.

Object difference

The C ++ programming model directly supports three programming models:

1. program model: the part from the C language;

2. abstract data type model: encapsulation and abstraction;

3. object-oriented model: defines the base class and derives the subclass.

Remember, writing a program purely as a model is conducive to good and stable overall behavior.

Polymorphism

The following methods in C ++ support polymorphism:

1. implicit conversion operation: converts the reference/pointer of the derived class to the reference/pointer of the base class;

2. virtual function mechanism: dynamic binding;

3. dynamic_cast and typeid operators.

So how much memory is required to present a class object?

  • The total size of non-static data members;
  • Added space to be filled by the need for annotation;
  • Add the burden to support virtual functions (for example, the pointer size pointing to the virtual function table ).

 

 

  

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.