Deep Exploration C + + object Model (6)

Source: Internet
Author: User
Tags continue object model

We're still dealing with constructors, and how do we not even think about constructors before we write a program? The original compiler has done so many things for us, we do not know. It seems that it will take some time to figure it out. We went down and went into a new chapter. Every time Thor finishes reading a chapter, Always look forward to the next chapter, because it means that another milestone has begun. This book is more intense because it is only 7 chapters in total.
At the beginning of chapter three, Thor was startled. The book gives an example:

Class x{};
Class Y:public virtual class x{};
Class Z:public virtual class x{};
Class A:public Y,public z{};

The results of the following are related to machines and to compilation, and different situations can produce different results. (How could it be?)

sizeof X; Result is 1
sizeof Y; Result is 8
sizeof Z; Result is 8
sizeof A; Result is 12

A class that does not have any members, the size of which is not 0.

Why?

First, there is no obvious class that contains members, its size is not 0, because in fact it is not empty, it is placed by the compiler a char, so that the two objects of this class can be allocated in memory unique address. As for the two derived classes Y and Z, because the burden of the language itself, There is also the compiler for the special case of the optimization process, there are alignment restrictions, so the result becomes 8. How does this 8 make up?

4 bytes to hold the pointer, what pointer? Pointer to virtual base class Subobject.

A char that is the same as Class X. It accounts for 1 bytes.

It was then subject to alignment, so it filled 3 bytes.

4+1+3=8

Note, however, that the results of different compiler Y and Z sizes are also different. Because the new compiler sees an empty virtual base class as the beginning of the derived class object, so the derived class has member, Therefore, it is not necessary to assign the bytes of char. There are no more than 3 bytes to fill, so it is possible that in some compilers, class Y and Class Z have a size of 4.

Finally, look at a. According to our analysis of Class Y, we can get the following formula:

4+4+1+3=12;

Not 16 of what we imagined, but 12. If you replace it with the new compiler we said above, the result is probably 8.

Thor 1, 4, 8 ... Said a bunch, do not know whether you understand or not, but this third chapter, read it really more than the first two chapters. We continue to look at the binding of data member, and now we only need to remember a defensive style to bind the members of a database: always place the declaration of a nested type at the beginning of the class to ensure the correctness of the non intuitive binding. Look at one of the following examples:

typedef int length; Zai
Class Point3D
{
Public
Length is resolved into global typedef or INT
_val was resolved into Point3d::_val
void mumble (length val) {_val=val}
Length mumble () {return _val;}
......
Private
Length must be seen before this class has its first reference operation
This declaration will make the previous reference operation illegal
typedef float Length;
Length _val;
......
};

How has become the transcription, the Thunder god also imperceptible, may be in this chapter's understanding to be easier, does not need to want to think a see to touch of the thing gestures. As if the children learn arithmetic, a number of calculations do not have to break the fingers, but the two-digit or three-digit calculation, finger plus toes is not enough. That's what learning is all about. Comprehension and abstract ability are very important. Come back and continue studying.

I also know the passage of this chapter. The layout of the data member. Access to data members. And there is a further understanding of the static data members that, in the life cycle of class, the static member is treated as a global variable, and the access of each one does not cause any additional burden of space or efficiency. Whether you inherit from a complex inheritance relationship or are declared directly, Static data member has only one entity. and has a very direct access path. In addition, if two classes declare a static member variable of the same name, then the compiler will solve the problem of name conflict by an algorithm. The existence of a non-static member variable is actually done by implicit the class object (this pointer). For example

Point3D
Point3d::translate (const Point3D &PT)
{
X+=pt.x;
Y+=pt.y;
Z+=pt.z;
}
Being transformed by the compiler into an internal transformation becomes the following:
Point3D
Point3d::translate (Point3D *const this,const Point3D &pt)
{
this->x+=pt.x;
this->y+=pt.y;
this->z+=pt.z;
}

If you want to access a non-static member variable, the compiler adds the starting address of the class object to the offset of the data member. For example:

Point3D origin;
origin._y=0.0;
Address &origin._y will be equal to
&origin+ (&point3d::_y-1);
The goal is to enable the compilation system to differentiate between the following two scenarios:
A pointer to a data member that indicates the first member of the class.
A pointer to a data member that does not indicate any members.
What does that mean? What is a pointer to a data member. The example in the book:
Class Point3D
{
Public
Virtual ~point3d ();
......
Protected
Static Point3D origin;//data member, position outside class object
Float x,y,z;//each float is 4bytes
}
&Point3d::z; What is this value?

We already knew at the beginning of this article that there was a vptr, but Vptr's position may be at the beginning of the object, perhaps at the end of the object. So the value of the above operation should be 8 or 12 (if vptr is in front). But actually the value of the fetch was added to 1. The reason is that you have to distinguish between a pointer that does not point to any member, and a pointer to the first member. And a little hard to understand, for instance:

Imagine you and your other two friends live in a three-room, one-bedroom house, where you live in the first room. If you give an address to a friend of your three, you can give the room number. Do not give the house number of any one of you (not pointing to any member). But if you give your personal friend an address, you will give the room number and your room number. To make this address different, you must have a hall as an offset. I do not know if you understand this example, perhaps this example will affect your correct thinking. That would be too bad. But I still like to think about it, it may not be accurate, but it can help me, because imagining a memory space is a bit harder than imagining a bedrooms.

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.