Illustrated C + + object model __c++

Source: Internet
Author: User
Tags object model
0. Foreword

The article is longer, and the content is relatively boring, I hope that the memory layout of C + + objects, virtual table pointers, virtual base class pointers have in-depth understanding of friends can slowly look. The conclusions of this paper are verified on VS2013. Different compilers may differ in the details of the memory layout.

If there are unexplained, unexplained or omissions in the article, please point out. 1. What is the C + + object model?

Quote the words "deep explore C + + object Model" in this book:

There are two concepts that can explain the C + + object model: The part of language that directly supports object-oriented programming. For the various supported underlying implementation mechanisms.

Direct support to object-oriented programming, including constructors, destructors, polymorphism, virtual functions, etc., which are discussed in many books, is also the most well-known place in C + + (characteristics). However, the underlying implementation mechanism of the object model is rarely discussed in the book. The underlying implementation mechanism of object model is not standardized, and different compilers have some freedom to design the implementation details of object model. In my opinion, object model studies the object in the storage space and time better, and the C + + object-oriented technology to support, such as virtual pointer, virtual table mechanism to support polymorphic characteristics. 2. Introduction to the content of the article

This article mainly discusses the layout of C + + objects in memory, which belongs to the research scope of the second concept. and C + + directly support the Object-oriented programming part is not much to say. The main contents of the article are as follows: virtual function table parsing. Contains a virtual function or its parent class contains a virtual function of the class, the compiler will add a virtual function table, vptr, first understand the composition of virtual function table, to help understand the C + + object model. Virtual base class table parsing. Virtual inheritance produces virtual base class table (VBPTR), the content of virtual base class table is completely different from virtual function table, we will introduce virtual function table when we explain virtual inheritance. Object Model Overview: Describes the Simple object model, the table-driven object model, and the C + + object model under inheritance. The C + + object model under inheritance. Analyze the memory layout of C + + class objects in the following scenario: Single inheritance: Subclasses are inherited from the parent class, and the class object memory layout is analyzed when the subclass overrides the parent class virtual function and the subclass defines the new virtual function. Multiple inheritance: subclasses inherit from multiple parent classes, analyze subclasses to override parent virtual functions, subclass define new virtual functions, object memory layout, and analyze the diamond inheritance under Non-virtual inheritance. Virtual inheritance: The virtual inheritance under single inheritance, the virtual inheritance under multiple base, and the virtual inheritance under repeated inheritance are analyzed. After understanding the memory layout of an object, we can analyze some of the problems: how much is the layout cost of the C + + package. The size of each class object in an inheritance hierarchy composed of empty classes.

As for other memory-related knowledge, I assume that we all have a certain understanding, such as memory alignment, pointer operation, and so on. At the beginning of this article, it may be obscure, require the reader to have a certain C + + base, the concept of a certain grasp. 3. Understanding virtual function Table 3.1. Polymorphism and virtual table

The function of virtual function in C + + is mainly to realize the polymorphic mechanism. Polymorphism, in simple terms, means that in an inheritance hierarchy, a pointer to a parent class can have many forms--when it points to a subclass object, it can call the function of the subclass, not the function of the parent class. C++

1 2 3 class Base {     virtual void print (void);     } class Drive1:public Base {     virtual void print (void);  &nbs p;  } class Drive2:public Base {     virtual void print (void);    &NB Sp
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.