Access to the 3.3 Data member is known as the following code:
Point3D origin;
origin.x = 0.0;
What is the access cost of x?
The answer depends on how X and Point3D are declared, X may be a static member, or it may be a nonstatic member. Point3D may be an independent (non-derived) class, or it may derive from another single base class, although it may even be inherited from multiple or virtual inheritance. The following sections examine each possibility in turn.
First look at such a question, if there are two definitions, Origin and PT:
Point3D origin, *pt = &origin;
Use them to access the data members, like this:
origin.x = 0.0;
pt->x = 0.0;
Is there a big difference between access via Origin and access via PT?
Static Data Member
The static Data member is extracted outside the class by the compiler and is treated as a global variable (but only visible within the class life range). Access rights for each member (private,public,protected) and Class is not associated with any extra burden on space or execution time-either in the individual class objects or in the static data member itself.
Each static data member has only one entity, which is stored in the program's data segment, and each time the program refers to the static member, it is internally converted into a direct reference operation to that unique extern entity, for example:
Origin.chunksize = 250;
Point3d::chunksize = 250;
Pt->chunksize = 250;
Point3d::chunksize = 250;
From the point of view of instruction execution, this is the only case in the C + + language where the "member by a pointer and access through an object is identical". This is because "Member selection operators (. operator) to a static data Member access Operation "is only a grammatical excision. Member is not actually in class object, so accessing the static members does not need to pass class object.
But what if Chunksize is a member inherited from a complex inheritance? Maybe it's a "virtual base class" member of virtual base class, and that doesn't matter, in the program for static Members still have only one entity, and the access path is still straightforward.
What if the access to the static data member is accessed through a function call? for example:
Foobar (). chunkSize = 250;
What happens when I call Foobar ()? Because ARM does not specify whether Foobar () must be evaluated, no one knows what will happen. Here is a possible conversion:
(void) foobar ();
Point3d.chunksize = 250;
If you take the address of a static data member, you get a pointer to its data type, not a pointer to its class member, because the static member is not contained in a class object, for example:
&Point3d::chunkSize;
Will get the following memory address of type:
const INT *
If there are two classes, each of which declares a static member FreeList, it causes a name conflict when they are placed in the program's data segment. The compiler's solution is to secretly encode every static data member To get a unique program identification code.
Nonstatic Data Members
nonstatic data members are stored directly in each class object, except through explicit (explicit) or implicit (implicit) class object, there is no way to access them directly. As long as the programmer is in a member function directly processing a nonstatic data member, the so-called "implicit class object" will occur, for example:
Point3D point3d::translate (const Point3D &pt) {
x + = Pt.x;
Y + = Pt.y;
z + = pt.z;
}
The apparent view of direct access to X, Y, Z is actually done by a "implicit class object" (expressed by this pointer). In fact, the parameters of this function are:
Internal transformation of member function
Point3D point3d::translate (Point3D *this, const Point3D &pt) {
This->x + = Pt.x;
This->y + = Pt.y;
This->z + = pt.z;
}
To access an nonstatic data member, the compiler needs to add the start address of class object to the offset of the data member. For example:
origin._y = 0;
Then the address &origin._y will be equal to:
&origin + (&point3d::_y-1);
Note that the-1 operation, which points to the data member pointer, is always prefixed with a value of 1, which allows the compilation system to differentiate "a pointer to data member to indicate class's first member" and "a pointer to the data Member pointers, there is no member in any of the two cases.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + object Model--data member access (chapter III)