Inside The C ++ Object Model Reading Notes (1)

Source: Internet
Author: User

I have never had a deep understanding of C ++. It seems that many cool people have read this Inside The C ++ Object Model. It seems that C ++ has to read this book in depth, so I decided to give it a try. After reading the directory on the day, you will know that this is definitely a good book. Many C ++ books do not know what the purpose is or do not want to be clear, or do not know clearly. In short, some mechanisms of C ++ are not written, even if it is written, there are many meanings that make people completely confused.

Okay, let's talk less. One of the reasons why C ++ has many things that are hard to understand is that C ++ has done too many things with us. This is my first response after I got this book.

The first solution is the C ++ object model. What is the C ++ object model like? I have no idea before reading this book. It may be related to my absence of compilation principles. Therefore, I am completely confused about the following problems:

                            ~           D:   function()=   (A)=? (B)=? (C)=? (D)=?

What is the size of an empty class or an empty Class Object? Why? What is the size of each class? Why? After reading the first chapter and contacting the third chapter, these questions become clear.

First, for an empty class, it is actually not empty, and it has a hidden 1 byte char. The reason for this design is, different objects of such a class can be configured on a unique address, which can be distinguished. So sizeof (A) = 1. This is one of the actions that the C ++ compiler does with us, that is, the so-called implicit code. The code written by the programmer should basically be called the explicit it code, while the code added by the compiler is called the implicit code.

For subsequent problems, we need to describe the C ++ object model. Take a Point class as an example: Point {

  static char _z;
  Point();

The class has two int type member variables, one declared static char member variable, one constructor and two virtual functions. Now let's take a look at its real object model structure:

  

That is to say, in a real object (non-empty), it does not declare anything, but only contains declared non-static member variables and a pointer to a Virtual table.

In this way, we can clearly understand the structure and size of the object. If the class does not declare or inherit virtual functions, the object size is the sum of non-static member variables (here we do not consider the issue of byte alignment). If there is a virtual function, a pointer to the virtual function table is saved in the object, this virtual function table is set to locate the virtual function, and a pointer occupies 4 bytes on a 32-bit machine, so the size of the pt object is 12 bytes. For the above example, Class B has two member variables, but static member variables are not stored in the object, so sizeof (B) = 4; class C has three non-static member variables, while the 32-bit machine is 4-byte aligned by default, so two char and one int occupy 8 bytes in total, sizeof (C) = 2 + 2 + 4 = 8; because D inherits C, sub-classes also have members of the parent class, and class D also has virtual functions, there must be a pointer to the virtual function table, therefore, sizeof (D) = 12.

So C ++ has done too many things on our back ~! Without understanding the compiler or design idea, you will always be stuck in the dark.

  

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.