In-depth exploration of the C ++ object model-object model, object model --

Source: Internet
Author: User

In-depth exploration of the C ++ object model-object model, object model --

C ++ Object Model

In C ++, there are two types of data members: static and nonstatic, and three types of classmember functions: static nonstatic and virtual

Now the Standard C ++ object model:

Nonstatic data Members is placed within each classobject, while static data members is stored outside all class objects. static and nonstatic function members are also placed outside all class objects, virtual functions is supported in two steps:

Each class generates a bunch of pointers to virtualfunctions, which are placed in a table called virtual table (vtbl)

Each classobject is added with a pointer pointing to the relevant virtual table. Generally, this pointer is called vptr. The vptr setting and resetting are automatically completed by the constructor, destructor, and copy assignment operators of each class, the type_info object associated with each class (used to support runtime type identification and RTTI) is also pointed out through virtual table, which is usually placed at the first slot of the table.

 

C ++ supports single-inheritance, multi-inheritance, and virtual inheritance. In the case of virtual inheritance, the base class is derived no matter how many times in the inheritance string chain, there will always be only one entity.

 

Although you can directly or indirectly process a base class object in the inheritance system, only the indirect processing through pointer or reference supports the polymorphism required by OO programming. The meaning of this sentence is that the objective of polymorphism can be achieved through pointers or references.

That is to say, the virtual mechanism can be implemented only through reference or pointer.

 

How much memory is required to present a class object?

The sum of nonstatic members:

Add any hesitant alignment (alignment) requirements to fill the space (may exist between members or the aggregate boundary)

Add any extra internal burden to support virtual.

 

Understanding of pointer types:

At the same time, it should be noted that a pointer (or a reference), no matter which data type it points to, the memory size required by the pointer itself is fixed. Therefore, the space occupied by pointers or references of any type is fixed.

Since the memory size of a pointer is fixed no matter which type it points to, how can we determine whether it is different when it points to different types of pointers? For example, is a pointer to an integer or a pointer to an object different?

From the perspective of memory requirements, there is no difference! All three of them need enough memory to store a machine address. The difference between pointers of different types is neither the pointer representation nor the content (representing an address) of different types, instead, the types of objects are different. That is to say, the "pointer type" will teach the compiler how to explain the memory content and its size in a specific address. That is to say, the compiler knows the size of the space pointed to by a pointer. For example, the size of space pointed to by a pointer pointing to an integer is 4 bytes. The size of the space pointed to by the pointer to an object is the size of this object.

Then the problem arises again. What kind of address space will a pointer pointing to address 1000 and its type is void? We don't know, that's why a pointer of the void type * can only contain one address, but cannot use it to operate the object. Therefore, the transformation is actually a compiler instruction. In most cases, it does not change the real address contained in a pointer, it only affects the Explanation of "memory size and content identified.

 

What if the pointer is used in the case of polymorphism?

Let's take a look at the role of pointers under polymorphism. If there is a base class ZooAnimal and a subclass Bear, then

Bear B;

ZooAnimal * pz = & B;

Bear * pb = & B;

In this case, the pz pb size is certain. Assuming that the address stored in B is 1000, what are the differences between the two pointers? Each of them points to the first byte of the Bear object. The difference is that the address covered by pb contains a real Bear object, while the address covered by pz only contains ZooAnimal subobject in the Bear object.

Except for members in ZooAnimal subobject, you cannot use pz to directly process any members in Bear. The only exception is the virtual mechanism. If the virtual mechanism is available, the type_info related to vptr will be viewed during execution.

A pointer or reference value supports polymorphism because they do not trigger any "type-related memory delegate operations" in the memory ", the only change is the memory size and content interpretation method they direct. That is to say, to different types of pointers, during compilation, the pointer determines the memory size and content interpretation method. For example, the pointer pointing to the int has been determined during compilation. The size of the pointer to the memory is 4 bytes and the content is an int. For example, the pointer pointing to struct has been determined during compilation. The Pointer Points to the memory size of sizeof (struct) and points to the content of this struct. When pointers participate in the virtual mechanism, it is another consideration.

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.