Data semantics--binding, layout, and access of members

Source: Internet
Author: User

Binding of data members

In order to be able to bind specific data members correctly, the earlier C + + compiler prescribes the following two behaviors:

    1. All data member must be placed at the beginning of the class declaration;
    2. All inline functions are placed outside the class declaration;

As you can see from the earlier compilers, the bound data member type must be determined after the class declaration is complete, but now the C + + compiler does not have to ask for the above provisions, or it can bind specific data members correctly, for example:

externfloat x;class Pointer{public:    Pointer(floatfloatfloat);    /* 这里返回的 x 是绑定哪个类型的? */    /* 现在的编译器可以确定绑定的是class 声明内部的 x 值 */    float X()const {return x;}    void X(float new_x)const{ x = new_x;}private:    float x,y,z;};

The name in the argument list of the member function in the earlier compiler is bound at the first encounter, for example:

int length;class Pointer{public:    voidnumble(length val){_val = val;}    length numble(){return _val};private:    float length;    length _val;};

From the code above, it is known that the binding void numble(length val){_val = val;} length numble(){return _val}; length is a int type, and when class appears nested type (nested type) typedef float length , the C++ standard previous binding is marked as illegal, so the earlier compiler will show class nested The type (nested type) is placed at the beginning of the class;

Layout of data members

The layout of the data members is:

    1. nonstatic data members are arranged in class object in the same order and in the order in which they are declared, and any static data member declared between them will not be placed in the object layout;
    2. The static data member is stored in the data segment of the program;

Where data segement is part of the program's virtual address space, contains global variables and static variables, whose size is determined by the variables placed by the programmer before the program is run.

The C + + standard stipulates that in the same access section, the private, public, protected, and so on, the members of the arrangement only need to conform to the late occurrence of the members in the class object has a higher address. The pointer vptr placement position of the virtual function table is determined by the compiler.

Access to data members

Access to data members varies according to the data type, which may be: static data member, nonstatic, data member, the class of which the member resides may be independent or derived (including: single inheritance, multiple inheritance, Virtual inheritance);

Static data members

The static data member is located outside of class object and has only one instance, which is stored in data segment. Regardless of whether the static data member is independent, or inherited, and regardless of the class in which the inheritance (including virtual inheritance) is located, any action on it will point to the same address in memory, which is determined at compile time;

nonstatic data members

nonstatic data member is stored directly in each class object, and access to it must be done through an explicit explicit or implicit implicit operation of object. The implicit implicit operation is that the operation of the data member is actually done through the this pointer when the member function uses the data member in the owning object. For an access operation to a nonstatic data member, the compiler needs to add the starting address of class object to the offset position of the data member, which is determined at compile time.

When the data member to be accessed is inherited from the virtual base class, the object and the access through the pointer vary greatly:

    1. When accessed through an object, the offset address of the member is determined at compile time, even if it inherits from the virtual base class.
    2. Access to a member variable can be determined at execution time, since it is not known exactly which object in the inheritance hierarchy is pointed to by the pointer.

Data semantics--binding, layout, and access of 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.