C + + object model: member variables < one > non-static Members

Source: Internet
Author: User

Non-static member variables, two possible, either class-custom, or inherited. Based on the "Deep Exploration of C + + object Model" interpretation.

class x{    private:    int  x, Y, z;};

In this class, there are three private member variables (whether private, protected, or public) that are arranged in a certain order ( generally based on the order of the definitions ), and the only thing to note is that some variables need to be aligned to fill. The order in memory is: x, Y, Z;
If you need to operate on these three variables, you will actually populate the member function with a pointer , referring to "easy to use MFC".

class x{    public:    void f ()/* filled here, get: void f ((x*) this)* /    {        x+ +;  /* equivalent to this->x++; */     }    private:    int  x, Y, z;};

So, how do you get the address of the member variable x? Uses: Object first address +offset (x), that is, the first address + Member variable offset, the composition gets the address of the member variable.

The second case: a member variable that has been inherited. In the same form, the inherited member variable is added to the memory block of the member variable of the object, and the order problem occurs. The answer to the book is: In general, the inheritance is in the front, the derived class is in the back ( this is verified by the program ).

C + + object model: member variables < one > non-static Members

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.