Inside The C ++ Object Model Reading Notes (3) -- storage of inheritance, data, and functions

Source: Internet
Author: User

IT's also a long time before I write a report, and I am busy with this broken thing. In fact, I often think that as a programmer or even a person in the IT industry, able to eat a book with peace of mind and without disturbing ideas

Or how happy it is to write a piece of code that satisfies you. Let's go straight to The theme. C ++ is designed to be profound and profound. Many people speak out C ++ for that reason. However, Inside The C ++

The author of the Object Model is Stanley B. lippman shows some seemingly confusing design reasons for C ++. I believe that some people who are biased against C ++ can view these causes rationally.

The design concept of C ++. This article mainly writes down some recent gains.

 

1. Let's first discuss the first small issue about inheritance:

Given two classes, class Point2d, class Point3d, and Point3d inherited from Point2d, how many member variables are there in Point3d, five or three? If there are five members, how can we tell whether the members x, y inherited from the parent class or their own x and y?

               Point3d:              };

It's easy to confirm this. Just use sizeof. The answer is sizeof (Point3d) = 20. In this way, we can confirm that it is five member variables and further instantiate a Point3d object:

Point3d ptr;

Use ptr. the method of x (using objects for direct access) is obviously the member x of the Point3d type called, rather than the x Member of Point2d, this is in line with the design idea (since it is a member of a specific unique class, of course it should be

Class members ). How can we access members inherited from the parent class?

You can do this: Point2d * pt = & ptr ;//

Pt-> x;

In this case, x is the member inherited from the parent class. In this way, we can draw a conclusion that the name of the member variable inherited from the parent class and the member variable name of the subclass itself can be repeated, and this is two different members.

 

 

  

Class B: public virtual father {...};

Class C: public A, public B {...};

Only one father class object is saved in the C object. If a class contains a virtual base class, it is divided into two parts: one unchanged area and one shared area. The data in the unchanged area is non-virtual base data, and the shared area is virtual base data. Class virtual function table pointer to the virtual function table, positive offset (down) is the addressing virtual function, negative offset (up) addressing is the virtual base class. Take Point2d and Point3d as examples. Point3d is inherited from Point2d, and pointer _ vptr_Point3d points to the virtual function table.

 


3. pointer to member variables

For an instance of class Point3d, & Point3d: z and & origin. the result of z is different. & Point3d: z should be the offset of member z in class Point3d, and & origin. z is the address of the member z of this instance.

However, in Microsoft Visual C ++ 10.0, the execution results of the following statements are very strange.

Printf ("% p \ n", & Point3d: x );

Printf ("% p \ n", & Point3d: y );

Printf ("% p \ n", & Point3d: z );

Cout <& Point3d: x <endl;

Cout <& Point3d: y <endl;

Cout <& Point3d: z <endl;

The first three values are 0, 4, and 8 (verified, that is, the offset address of the member variable, expected). The values in BCB3 are 5, 9, and D, respectively, I did not verify it; the last three results are 1 very strange. The reasonable explanation can only be that VC ++ has had special processing.

Now, let's write it here. continue learning ~

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.