C ++ object model notes: overview

Source: Internet
Author: User
This series of articles is a book note for Lippman's "inside the C ++ object model.

1. Books on C ++ made by author Lippman: C ++, one of the first compilers:

C ++ Primer: The Encyclopedia of C ++, which is the same name as C ++ programming language, the father of C ++.

Inside the C ++ object model: Describes the underlying mechanism of the C ++ syntax.

Essential C ++: an entry-level book of C ++ is also a subset of C ++ primer.

2. What is a C ++ object model?

My understanding is that the implementation mechanism behind the compiler of C ++ syntax, such as virtual functions, can be used to implement polymorphism, and how the compiler calls the polymorphism of virtual functions. (You do not need to understand every detail. You only need to understand the principle)

3. Why do we need to learn the object model?

The author's reason for Lippman: it is the basis of component programming (for example, COM, CORBA, Som, etc.) models, and has learned why it can be more effective for C ++ to program.

My opinion is: "You should know the truth, because the truth can free you ".

4. This book explores the C ++ Object Model in depth. What are the underlying mechanisms of C ++?

As follows:

Constructor, destructor, copy constructor, and assign value operator (Chapter 2 and Chapter 5)

Usage of new and delete, temporary object, object array, return value optimization, static local variables (Chapter 6)

Template, exception handling, rtti (chapter 7)

General class, virtual, virtual base class, multi-inheritance and other detailed descriptions of the Object Memory layout (Chapter 3)

A detailed description of common class, virtual, virtual base class, and multiple inheritance function calls (chapter 4)

Other...

5. Reasons for making decisions based on the underlying mechanism of C ++ syntax

Many C ++ decisions are based on the following two factors:

(1) Try to be compatible with C

(2) try to achieve the efficiency of C. The efficiency mentioned here is the time efficiency and space size of the runtime. Note that it is the runtime, not the compilation time. You can say that, c ++ sacrifices the compilation time in exchange for efficiency during runtime.

6. Some instructions

(1) concerning the underlying implementation mechanism of the C ++ syntax, in fact, the C ++ standard does not specify how to implement it, such as virtual functions. You can use the location lookup table, you can also use the Name Lookup Table (as mentioned in the notes next to the specific mechanism) to implement virtual functions. The C ++ standard does not describe, each compiler can have its own practice, but on the other hand, the consequences of competing compilers are false, the underlying implementation of the c ++ virtual function uses the absolute offset position to find the table (this method is more efficient, of course, it also has its disadvantages ).

That is to say: due to the competition between compilers, the implementation mechanism behind C ++ is similar and moving toward an efficient goal (if you do not understand this truth, you can read the western micro-economics principles, which are described in detail)

(2) Relationship between (space-time) efficiency, universality, and indirect Layer

If it is widely used, the indirect layer will be added (many technical problems are solved by adding the indirect layer), which will lead to lower efficiency;

If high efficiency is required, the indirect layer must be less (or even cannot be used), which leads to poor universality;

Applications in C ++ are embodied in many articles in the future.

7. One example (the example in the book)

Before taking notes, write an example in the book. If you are interested in this example, this book (inside the C ++ object model) will be very suitable for you (because this book is about this aspect ).

  1. Class X
  2. {
  3. Public:
  4. X () {...} // Constructor
  5. X (const X & X) {...} // copy the constructor
  6. Virtual ~ X () {...} // virtual destructor
  7. Virtual void Foo () {...} // virtual function foo
  8. ...
  9. };
  10. X foobar () // a global function foobar
  11. {
  12. X xx;
  13. X * PX = new X;
  14. // Foo () is a virtual function.
  15. XX. Foo ();
  16. PX-> Foo ();
  17. ...
  18. Delete PX;
  19. Return xx;
  20. }

The pseudo code of the above function foobar after compilation is as follows (Note: It is pseudo code, because the compiled code is a machine code, it is more convenient to use the C pseudo code here)

  1. // After compilation, the C ++ pseudo code of the foobar function body
  2. Void foobar (X & _ result)
  3. {
  4. // Converts x XX statements.
  5. // Because the final returned result is XX, the return value is optimized here, instead of XX with _ result.
  6. _ Result. X: X ();
  7. // Convert x * PX = new X;
  8. Px = _ new (sizeof (x ));
  9. If (0! = Px)
  10. PX-> X: X ();
  11.  
  12. // Convert XX. Foo ()
  13. Foo (& result );

  14. // Convert the PX-> Foo () Statement, where _ vtbl is the legendary virtual function table.
  15. (* PX-> _ vtbl [2]) (PX );
  16. // Convert Delete PX
  17. If (0! = Px)
  18. {
  19. (* PX-> _ vtbl [1]) [PX];
  20. _ Delete (PX );
  21. }
  22. // For the return XX conversion, the return value has been returned by referencing the parameter _ result.
  23. Return;
  24. }

In fact, after compilation, the conversion of the foobar function is more than that, but these are enough to explain the problem.

If you see this, you are curious: "Why is the compiler doing this ?", Then you can look down,

The following will open the door for implementing the underlying C ++ language, so that you can see the underlying factory of the C ++ language. (Of course, the best way is to read the book. If you don't have time, read the articles in this series .)

8. Learning Methods

Let's talk about how I learned the underlying model of C ++, that is, reading a book, writing some code, and disassembly and analyzing some examples. In this way, I am deeply impressed. Some articles aim to visualize them, I drew some images and understood them very well. I knew what was going on at first glance, but this was just an intuition. Maybe I forgot everything in an hour. Therefore, the best way is to write code, decompile these conclusions, and draw a picture by yourself. This is quite impressive.

9. Running environment of the example

The example in my notes uses a 32-bit intel X86 CPU. The platform is either Windows vs2005 or G ++ 3.4.3 under fedara Linux, the basic data type is as follows:

Bool, char is 1 byte; short is 2 byte; int, long, float is 4 byte

Double and long are all 8 bytes pointer types: 4 bytes

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.