Exploring the C + + object model in Depth--chapter III Data semantics

Source: Internet
Author: User

Note: The following part of the picture from the original book

1. With regard to the size of the class, there are three factors that affect the size of the class:

A. The additional burden created by the language itself, such as the virtual base class, the corresponding subclass will have some form of pointer, either directly to the virtual base class child object, or to a table that holds the virtual base class child object address or offset. For example, a virtual function that inherits from a subclass containing a virtual function base class contains a pointer to the virtual function table. In short, these will increase the size of the class object.

B. Alignment operations, that is, in order to achieve the highest transmission efficiency of the bus, the class size is aligned, adjusted to a multiple of a number size, 32-bit machine, usually a multiple of 4, that is, a class's actual occupancy size is 3, then it will be adjusted to 4, something similar.

For example, the following example:


C. Compiler optimizations for special cases, such as for virtual base classes with size 1, some compilers provide special support, in which case the empty virtual base class is treated as the very beginning part of the subclass object, so that no extra space is occupied, and the size of the class Y and Z under this model is as follows:


Note in virtual inheritance, regardless of how many times the virtual base class appears in the inheritance system, it always has only one copy in the subclass, this is also one of the reasons for the origin of virtual inheritance, so for Class A, the first is the virtual base class X size 1, plus the size of y minus the size that is configured because of the virtual base class X (because the virtual base class has only one copy in the subclass, So to reduce the size in Y), the result is 4, the same z size is 4, the current Class A size is 9, after the alignment is a (VS2015 test is 8, presumably because of the heap void base class optimization).

2. Access to data members.

A. For static data members, the use of pointer operations and object manipulation efficiency is the same, for example: A.staticmember = 0 and Pa->staticmember = 0 are transformed into: A::staticmember = 0, so the two are the same. In addition, taking an address to a static member gets a pointer of the appropriate type, rather than a pointer to a class member (a detailed record of the concept of a class member pointer is followed).

B. For non-static members, when manipulating data members through class objects, the compiler adds the starting address of the class object to the offset of the class member, for example: a._y=0.0, then the address &a._y equals &a + (&a::_y-1), (The pointer offset to the class member is always added 1, this is followed by a detailed record, if the data member of the inherited virtual base class (the non-virtual base class does not have indirection) in the subclass is required to have a layer of indirection, which will then be sorted out in detail.

3. For Operations a.x = 0.0 and pa->x = 0.0 efficiency is not the same in the following cases: When x comes from the virtual base class, at compile time, we cannot have the exact type of the PA (which only knows the static type, not the dynamic type), he may point to a base class, or it may point to a subclass. Therefore, it is not possible to know exactly where x is offset, so the access operation must be deferred to the execution period and solved by an additional introductory guide. If you use a, there is no such uncertainty, because the type of a is OK to know, so the offset of the member is determined at compile time.

Note: For static and dynamic types of pointers, for example:

b b; b public Inheriance of A
* PA = b;
At this time the PA static type is a, dynamic type B, that is, static type fixed invariant, and dynamic type is variable, in fact, C + + mechanism is the parent pointer to different subclass object, transform its dynamic type implementation.

The follow-up will continue to organize the rest of this chapter.








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.