Explore the C + + object model in depth--object model

Source: Internet
Author: User

C + + object model

There are two types of data members in C + +: Static and nonstatic, and three kinds of classmember functions:static nonstatic and virtual

Now the standard C + + object model:

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

Each class produces a bunch of pointers to virtualfunctions, placed in a table, called Virtual table (VTBL).

Each classobject is added with a pointer to the associated virtual table, which is usually called vptr,vptr settings and resets by each class of constructor, destructor, and copy The assignment operator is automatically completed, and the Type_info object associated with each class (to support runtime type Identification,rtti) is also pointed out via virtual table. Usually placed in the first slot of a table.

C + + can support single-inheritance, multiple inheritance, virtual inheritance, in the case of virtual inheritance, the base class regardless of the inheritance chain in the derivation of how many times, there will always be only one entity.

Although you can deal directly or indirectly with a base class object in the inheritance system, the polymorphic nature required for OO programming is supported only through the indirect processing of pointer or reference. The meaning of this sentence is that you can use pointers or references to accomplish polymorphic purposes.

That is, the virtual mechanism can be implemented only through references or pointers.

So how much memory does it take to represent a class object?

The total size of its nonstatic members:

Plus any hesitation alignment (alignment) to fill the space (padding) up (may exist between members, may also exist in the aggregate boundary)

Plus any additional burden generated by the interior in order to support virtual.

For the understanding of pointer types:

It is also necessary to note that a pointer (or a reference), regardless of which data type it points to, the size of the memory required by the pointer itself is fixed. So for any type of pointer or reference, the amount of space it occupies is fixed.

Since a pointer is a fixed amount of memory regardless of which type it points to, how can you tell the difference between pointers to different types? For example, a pointer to an integer or a pointer to an object is different?

From the memory requirements point of view, no difference! All three of them need to have enough memory to place a machine address. The difference between "pointers to different types" is not the same as its pointer notation, nor its content (representing an address), but rather the type of object it addresses, that is, the pointer type teaches the compiler how to interpret memory content and its size in a particular address. That is, the compiler knows the size of the space that a pointer points to. For example, a pointer to an integer pointing to the size of the space is 4 bytes. The size of the space that the pointer to an object points to is the size of the object.

So the question comes again, a pointer to address 1000 and type void*, what address space will be covered? We do not know, that is why a pointer of type void* can only contain an address, and cannot manipulate the object it refers to. So, the transformation is actually a compiler directive, in most cases it does not change the actual address contained in a pointer, it only affects the "size and content of the indicated memory" and the way it is interpreted.

What if you use pointers in polymorphic situations?

To see the role of pointers in polymorphic situations, if there is a base class Zooanimal, a subclass bear, then

Bear B;

zooanimal* PZ =&b;

bear* pb=&b;

As a result, the size of the PZ PB is certain, assuming that B stores the address is 1000, then what is the difference between the two pointers? Each of them points to the first byte of the Bear object, except that the address covered by PB contains the total Bear object, and the address contained in the PZ contains only the Zooanimal subobject in the.

In addition to the members appearing in Zooanimal subobject, you cannot use PZ to directly deal with any members of the bear, except through the virtual mechanism. If there is a virtual mechanism, it will be viewed at the time of execution in vptr-related type_info.

A pointer or a reference value therefore supports polymorphism because they do not cause any "type-related memory delegate operations" in memory to be changed only by the "size and content interpretation" of the memory they point to. That is, pointing to different types of pointers, which during compilation determines the size of the memory that the pointer points to and how the content is interpreted. A pointer to int, for example, has been determined at compile time that the pointer is pointing to a memory size of 4 bytes with an int. A pointer to a struct, for example, is determined at compile time, the pointer to the memory size of sizeof (struct), the content of the struct is pointed to. It is another consideration for pointers to participate in the virtual mechanism.

Explore the C + + object model in depth--object model

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.