Starting your headers -- about VC ++ object models

Source: Internet
Author: User
A c ++ programmer should learn more about the semantic details of some languages to further improve the technical level. For programmers who use VC ++, they should also understand some VC ++ interpretations of C ++. Although the inside the C ++ object model is a good book, it has a lot of space and has little to do with the specific VC ++. Therefore, from the perspective of length and content, the translator believes that this article is a good starting point for a better understanding of the C ++ object model.

This article was previously well read. I had to re-read the old document and understand more. As a result, I came up with an idea to translate and share it with you. Although the article is not long, but the time is limited, and several times during translation nap and fall asleep, dragging and pulling for a small month.

On the one hand, due to my own level limitations, on the other hand, due to frequent dotting during translation, I am afraid there are many mistakes. You are welcome to criticize and correct me.

1 Preface

Understanding how the programming language you use is actually implemented may be particularly meaningful to C ++ programmers. First, it can remove the mystery of the language in use, so that we do not feel completely unbelievable For the compiler to do the work; especially, it gives us more control when debugging and using advanced language features. This knowledge can also help us to improve code efficiency.

This article focuses on answering the following questions:
* How are classes laid out?
* How to access member variables?
* How to access member functions?
* What is the so-called "adjustment block" (adjuster thunk?
* What is the overhead when the following mechanism is used:
* Single inheritance, multi-inheritance, and virtual inheritance
* Virtual function call
* Forced conversion to the base class or forced conversion to the virtual base class
* Exception Handling
First, we examine the layout, single inheritance, multi-inheritance, and virtual inheritance of the C-compatible structure (struct) in sequence;

Next, let's talk about the access to member variables and member functions. Of course, there are virtual functions in this section;
Next, we will examine how constructor, destructor, and special value assignment operator member functions work, and how arrays are dynamically constructed and destroyed;
Finally, we will briefly introduce the support for exception handling.

For each language feature, we will briefly introduce the motives behind this feature and the semantics of this feature (of course, this article is by no means a "C ++ getting started" and you should fully understand it ), and how this feature is implemented in Microsoft's VC ++. Note that the abstract C ++ language semantics is distinguished from its specific implementation. Other C ++ vendors outside of Microsoft may provide a completely different implementation. We occasionally compare the implementation of VC ++ with other implementations.

2 types of Layout

This section describes different memory la s caused by different inheritance methods.

2.1 C structure (struct)

Since C ++ is based on C, C ++ is also "basically" compatible with C. In particular, the C ++ specification uses the same structure as C. The simple memory layout principle is that member variables are arranged in the declared order, alignment on the memory address according to the specific implementation principle. All C/C ++ vendors ensure that their C/C ++ compilers adopt the same layout for valid C structures. Here, A is a simple C structure with clear Member layout and alignment.

Struct {
Char C;
Int I;
};

Note: As you can see, a occupies 8 bytes in the memory. In the order of declaring members, the first 4 bytes contain one character (actually occupying 1 byte, the three bytes are empty, and the last four bytes contain an integer. The pointer of a points to the starting byte of the character.

2.2 C structure with C ++ features

Of course, C ++ is not a complex c. c ++ is essentially an object-oriented language, including inheritance, encapsulation, and polymorphism. The original C structure has been transformed to become the cornerstone of the object-oriented world-class. In addition to member variables, the C ++ class can also encapsulate member functions and other things. However, it is interesting that unless the hidden member variables introduced to implement virtual functions and virtual inheritance, the size of the C ++ class instance depends entirely on the member variables of a class and its base class! The member functions basically do not affect the class instance size.

The B provided here is a C structure. However, this structure has some C ++ features: "Public/protected/private" keywords that control member visibility, member functions, and static members, and nested type declaration. Despite the wide array of data, only member variables occupy the space of class instances. Note that the C ++ Standards Committee does not limit the sequence of each segment separated by the "public/protected/private" keyword during implementation. Therefore, different compilers may implement different memory la S. (IN Vc ++, member variables are always arranged in the declared order ).

Us) Jane Gray
Cheng Hua Translation
Click to view the full text:
Http://blog.csdn.net/xueyong1203/archive/2009/05/23/4211023.aspx

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.