MinGW is exploring how to store objects, how to achieve class inheritance, polymorphism

Explore MinGW is how to store objects, how to achieve class inheritance, polymorphism

First, the needs analysis

Objects, inheritance, polymorphism is some of the concepts of object-oriented programming language, MinGW just a build environment, it can compile C ++ programs, C ++ is an object-oriented programming language, it is more correct to say that "C ++ is how to store objects, how to achieve class inheritance, polymorphism. "

1, C ++ class data members stored distribution

2, C ++ class virtual function storage allocation

3, C ++ class inheritance storage allocation

4, C ++ polymorphism

Second, the outline design

C ++ based on the C language adds object-oriented programming, C ++ supports object-oriented programming. C ++ class is a core feature, often referred to as user-defined type. Class is used in the form of the specified object, the method comprising the data processing method for data representation. Class data members of the class and the method is called. Functions are called class members in a class.

Class provides the blueprint for an object, so basically, is to create objects based on class. Object class declaration, as declared basic types of variables the same.

Object-oriented programming is the most important concept is inherited. Another class inheritance allows us to define a class basis, which makes creating and maintaining an application easier. In doing so, also reached the reuse of code and improve the execution time of the function effect.

When there is a hierarchy between classes, and inheritance between classes through the association will be used polymorphism. When C ++ member function calls polymorphic means that will perform different functions depending on the type of the object function is called.

Third, the detailed design

1, the basic data types of byte occupied by

I use a 32-bit compiler environment MinGW
 char:. 1 byte, char * (i.e. pointer variable): 4 bytes, short int: 2 bytes, int: 4-byte, unsigned int: 4 th byte, float: 4 bytes, double: 8 bytes, long: 4 bytes, long long: 8 bytes, unsigned long: 4 bytes

2, the object occupies a certain amount of memory, the memory is stored in the object-related data, according to the following sequence:

(1) the virtual table pointer: if the class has virtual functions, it will store the virtual table pointer, the pointer to the vtable of the class, i.e., points to the first element in the table. Virtual table is the address stored in the class virtual functions;

(2) the base class data members (if any base class);

(3) own data members;

3, C ++ memory alignment

Alignment is to increase efficiency, fetch the data from computer memory in accordance with a fixed length. In 32-bit machine, for example, it takes 32 each, which is 4 bytes (8 bits per byte). Benefits byte-aligned, double-data to an example, if the location of it is stored in memory by a 4-byte aligned, i.e. all the data in a double take a computer falls within the interval number, then only need to take it once. If not aligned, the double data just across the border to take the number, so need to take twice to double this to get all the data, so that efficiency is reduced.

4, memory alignment rules

(1) data member alignment rules

Structure (struct or union) of the data member, a first local data member in offset 0, the data after the start position of each member of the sub-member from the stored size of the member or members of the size (so long as the sub-members members, say arrays, structures, etc.) is an integral multiple start (int 32-bit machine, such as 4 bytes, then addresses starting from an integral multiple of storing 4), does not include basic types struct / class / uinon.

  1. As a member of the structure

If there are a certain structural body structure member, the structure member from the inside "broadest basic types of members" storage start address of an integral multiple (struct a where there struct b, b there char, int, double, etc. element, then b should be an integer multiple from the start of the memory 8).

  1. Finishing work

The total size of the structure

  1. sizeof(union)

5, C ++ class inheritance storage allocation

(1) C ++ class virtual function storage allocation

The destructor into a virtual, i.e. the introduction of virtual functions in the class, for future application polymorphic compiler adds data member before the vptr a pointer, pointing to the vtbl (table stores address information of each virtual function) .

So, in addition to the space occupied by the object inserted into the safety of space allocated to the data members, as well as according to the compiler.

(2) C ++ class inheritance single storage allocation

In this case, the object subclass can be directly converted to the parent object, since the memory layout subclass object viewed from above is the parent of the data, the following data is newly added subclass object. The conversion process is only assigned to the subclass object pointer to the parent object pointer process. But to convert the parent object to subclass object is very dangerous, and it is generally not allowed. Getting local proxy object in android binder mechanism, it is to get the top-most parent class IBinder reference, in fact, address this is a subclass object.

(3) C ++ multiple inheritance class storage allocation

Multiple inheritance refers to the inheritance when a call this subclass have two or more direct parent class for multiple inheritance. This inheritance make a plurality of subclasses inherit the characteristics of the parent class. Multiple inheritance single inheritance can be seen as an extension. Derived class having a plurality of base classes, derived class relationship between each of the base class can still be considered a single inheritance. Multiple Inheritance Constructors and orders derived class to inherit under the derived class constructor is similar, it must also be responsible for all calls to the base class constructor of the derived class. Meanwhile, the number of parameters derived class must contain the number of parameters needed to complete the base class for all initialization. Subclass are stored in memory in the order of declaration definition

The order of succession of the distribution and declared subclass object-related data, the closer the above subclasses

(4) C ++ class inheritance + virtual memory allocation

The destructor into a virtual, i.e. the introduction of virtual functions in the class, for future application polymorphic compiler adds data member before the vptr a pointer, pointing to the vtbl (table stores address information of each virtual function) . So, in addition to the space occupied by the object inserted into the safety of space allocated to the data members, as well as according to the compiler.

C ++ class has virtual functions when there is a pointer pointing to a virtual function (vptr), the system assigns the 32-bit pointer size of 4 bytes. No matter how many virtual function, it is only a pointer, 4 bytes. // Note that there is no general function pointer, and does not account for memory class.

In C ++, if the class has virtual functions, then it will have a virtual function table pointer __vfptr, memory data in the beginning of the class object. After the memory data member variables of the class. For subclasses, the beginning of the memory data recorded copy of the parent object class (including the parent class virtual function table pointer and member variables). After a subclass of its own member variable data. For the subclass subclass, it is the same principle. But no matter how much subclass inherits the object is always only a virtual function table pointer. 

(5) C ++ base class virtual storage allocation

Virtual inheritance is an object-oriented programming technique, refers to a designated base class in the inheritance hierarchy structure, whose members share the instance data from the other classes to be directly or indirectly derived base type, which can be shared characteristics, avoiding the problem of multiple copies of the same data, so as to solve the ambiguity diamond inheritance and data redundancy.

Virtual base classes are not declared in the base class declaration, but the statement specifying inheritance at the derived class declaration, as a base can be used as a virtual base class that is derived in a derived class, the derived class at a derived another It is not a virtual base class,

The general form of declaration virtual base class: class derived class name: viryual inheritance of a base class name

  1. C ++ polymorphism

When there is a hierarchy between classes, and inheritance between classes through the association will be used polymorphism. When C ++ member function calls polymorphic means that will perform different functions depending on the type of the object function is called.

When generating a new object, the compiler inserts in the constructor initialization code, initializes the vptr, so vtbl vptr point class. In this way, we can achieve polymorphism.

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/81258572