Encapsulation, inheritance, and polymorphism (on) in C ++ memory)

Source: Internet
Author: User

 

I. encapsulation model memory Layout

A member of a common class object may contain the following elements: built-in type, pointer, reference, composite object, and virtual function.

Another angle of classification:

Data member:

Member functions:

 

 

The built-in types in the class are stored continuously in the memory in the declared order, and the allocation size is determined by the size of the built-in types (dependent on machines). The layout is affected by the byte alignment.(This article does not discuss byte alignment).

 

&*     (*

When the storage mode is the same as 1, the difference is that pointer and reference are usually fixed size (32-bit machines 4 bytes, 64-bit machines 8 bytes ).

Reference: the reference that I personally understand is a special pointer for a lazy person. It is very troublesome to take the address and try again.Constant pointer to a constant that automatically retrieves an address from another address..

The declaration in the class can measure the fixed byte size, so it also occupies a fixed byte size.

 

 

Memory layout diagram (this article and subsequent articles use 32-bit Win7, VS2008 ):

Let's take a look at the address:

CONCLUSION: (obviously not explained)

Class Object is finally interpreted as a built-in type, LayoutStillIn the declared order, and the objectThe layout is still continuous in the memory.

 

  

Memory Layout

 

Take a look at the program output

typedef  (*<<<<(*)&t<<<<<<(*)*(*)&t<<<<= (PF)*(*)*(*)&<<= (PF)*(*)*(*)& 

Output illustration

Proof of reasoning:

1.The address obtained after the t is output to the (int *) type = The vfptr address of the t (the first line of the debugging window ):The virtual function pointer is placed at the first address of the object layout.

2.Because (int *) & t = vfptr, what * vfptr obtains isVirtual function table.

(Int *) * vfptr, SetVirtual function tableThe address of the first address to be forced to (int *) = the address of the virtual function table of the t object _ vftable (Line 4 of the debugging window ):Virtual function pointer pointing to virtual function table

3.There is a lot of difference between the first address of vftable and the address of the first function of vftable:The virtual function table also undertakes content other than the virtual function.

What content will be stored in the virtual function table?

The virtual function table is used to implement polymorphism. polymorphism means that the type is fuzzy. After blurring, you must record your old database; otherwise, you cannot implement another thing --RTTI.

Conclusion:

A vfptr is added when a virtual function is included. It is a const pointer located at the first position in the class layout and points to the virtual function table. The virtual function table contains the virtual function address, use the virtual function address to access the virtual function.

In addition, the first address of the virtual function table has the type information of this class, which is used to implement RTTI.

 

 

Static features are well known. It is not possible to draw any conclusions from the variables observed in the debugging window. We will first list several features:

1. The static member is a common attribute of the entire class.

2. The static function does not contain the this pointer.

3. static members cannot access nonstatic members.

Preliminary conclusion:

In the memory object model, static is isolated (not all objects have), and static is exclusive.

 

Build the C ++ encapsulation model based on the preceding five items:

 

Common member functions

The so-called class is to define a domain name, so the code area in the memory also defines its own domain, where ordinary member functions are placed.

Static member functions

The idea of setting a static region in a class domain name in the code area is still dominant.

Constructor

Due to the special nature of the constructor, the code area has its own constructor code area.

Now we have a more complete model:

Assuming that the reader already understands the heap/stack/static zone and constant zone/code Zone

 

 

Based on our conclusions

1. The class is eventually interpreted as a built-in type (the built-in type will no longer exist after the compilation period ends, but it is just the interpretation method during the compilation period)

2. built-in types are stored in declared order

3. If virtual functions exist, vfptr is generated and vfptr-> vtable-> function ()

4. static members are treated separately, data is copied only once, and functions are put in the static area.

5. Type Infomation is put into vftable

 

Ii. Construction Process of encapsulation model

1. static data is determined during the compilation period. All objects are copied to each other and are created first.

2. Go to the constructor and create vfptr and vftable first, that is, create virtual functions first.

3. Construct Data members in the declared order.

We can useExpressionTo do something interesting.

We need to define

(* PF )();
PF pf = NULL;

<<, )), a((cout<<,   fun(){cout<<<<=(PF)*(*)*(*), pf()), cout<<, )), data2((cout<<,     fun(){cout<<<<  T::sdata1 = (cout<<, );

 

The following is the result of the program running:

Static -- virtual function table -- Declaration Order initialization.

 

The article has some negligence and shortcomings. You are welcome to criticize and correct it. Mailbox [betachen@yeah.net]

The next article focuses on the memory layout during inheritance and polymorphism.

 

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.