"Deep Exploration C + + object Model"-about objects (object lessons) __c++

Source: Internet
Author: User
Tags object model
Preface

"This book is written by a compiler designer for senior C + + programmers. The assumption behind this book is that programmers who understand the C + + object model can write code that is less prone to error and more efficient. ”

It is because of the loss in the actual programming that I feel that reading this book is very necessary, such as a previous blog post:

http://blog.csdn.net/jiange_zh/article/details/51871782

If you had read this book, you would not have been confused.

Here are some of my personal reading notes, most of which may be extracted from the book, only to facilitate the future of their own quick review. 1th chapter on objects (object lessons) plus the layout cost after encapsulation

The main additional burden of C + + in layout and access time is caused by virtual, including:

virtual function mechanism--to support an efficient "execution binding" (runtime binding);

Virtual base class--is used to implement the base class, which appears multiple times in the inheritance system, with a single and shared entity.

In addition, there are additional burdens under multiple inheritance that occur between "a derived class and its second or subsequent base class conversion". C + + object model

In C + +, there are two kinds of class data members:static and nonstatic;
and three class member Functions:static, NONSTAITC, and virtual.

The following class point declarations are known:

Class Point  
{public  
: Point  
    (float xval); 
    Virtual ~point ();

    float x () const;  
    static int Pointcount ();

Protected:  
    virtual ostream&  print (ostream &os) const;

    float _x;
    static int _point_count;  
};

How the class will behave in the machine.

The nonstatic data members are configured within each class object;

The Static data members are stored outside of all class object;

The Static and nonstatic function members are also stored outside of all class object;

Virtual functions is supported in two steps:

1 Each class produces a bunch of pointers to the virtual functions, placed in the table, which is called virtual table (VTBL);

2 Each Class object is added a pointer to the associated virtual table, which is usually referred to as vptr. Vptr settings and resets are automatically completed by the constructor, destructor, and copy assignment operators of each class. The Type_info object associated with each class (to support the runtime type Identification,rtti) is also pointed through virutal table, usually at the first slot of the table. the memory size of a class object

Generally speaking, there should be:
1. Sum of the size of its nonstatic data members;
2. Plus the memory size to be filled by the memory alignment; (for completion, as described in the book "A deep understanding of computer systems")
3. Add any additional burdens that arise from the interior in order to support virtual.

A pointer, regardless of which data type it points to, the memory size of the pointer itself is fixed.

However, how does a pointer to class test differ from a pointer to an integer?
From the perspective of memory requirements, there is no difference, they all need enough memory to store a machine address .
They differ in that the object type of addressing is different, that is,"pointer type" teaches the compiler how to interpret the contents of memory and its size in a particular address.
For a void* pointer, because it has no specific type, the compiler does not know how to interpret it, which is why a pointer of type void* can only hold an address, but it cannot be manipulated by the object it refers to.

for Transformation (cast), it is actually a compiler instruction , most of the time does not change the real address of a pointer, it only affects "the size of the object memory and its content" interpretation. after adding polymorphism

Suppose there is a base class Zooanimal (16bytes), and a class bear (8bytes) that inherits from Zooanimal.

Bear B ("Yogi");
Bear *PB = &b;
Bear &rb = *PB;

B, PB, RB memory requirements:

Either pointer or reference (which is actually implemented by pointers) only requires a word space (4bytes on 32-bit machines). The Bear object requires 24bytes, including Zooanimal 16bytes and the bear is brought by 8bytes.

Suppose our Bear object is placed at address 1000, and a bear pointer differs from a zooanimal pointer.

Bear B;
Zooanimal *pz = &b;
Bear *PB = &b;

They all point to the first byte of the Bear object, the difference being that PB covers an address range that is the entire bear object, and the address covered by PZ contains only the Zooanimal component of the containing object.

You cannot use PZ to directly handle any members of the bear, except for members appearing in the Zooanimal component. The only exception is through the virtual mechanism.

A pointer or a reference supports polymorphism because they do not raise any type-related memory delegate operations in memory, but are affected only by the "size and content interpretation" of the memory they point to.

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.