Wednesday, June 13, 2007

3.3 Access of a Data member

Static Data Members

Static data members are literally lifted out of their class and treated as if each were declared as a global variable (but with visibility limited to the scope of the class). This is the only case in the language where the access of a member through a pointer and through an object are exactly equivalent in terms of the instructions actually executed.

Taking the address of a static data member yields an ordinary pointer of its data type, NOT a pointer to class member, since the static member is not contained within a class object.

Name-mangling is used to yield a unique program identifier if two class declare the same static variable.

Nonstatic Data Members

Nonstatic Data members are stored directly within each class object and can NOT be accessed except through an explicit or implicit class object. An implicit class object (represented by the this pointer) is present whenever the programmer directly accesses a nonstatic data member within a member function.

The offset of each nonstatic data member is known at compile time, even if the member belongs to a base class subobject derived through a single or multiple inheritance chain. Access of a nonstatic data member, therefore, is equivalent in performance to that of a C struct member or the member of a nonderived class.

Virtual inheritance introduces an additional level of indirection in the access of its members through a base class subobject.

Point3d origin;
Point3d *pt;
origin.x = 0.0;
pt->x = 0.0;
acess of those two methods are different?

The answer is the access is significantly different when the Point3d class is a derived class containing a virtual base class within its inheritance hierarchy and the member being accessed, such as x, is an inherited member of that virtual base class. With the object origin, the offset location of even inherited virtual base class members are fixed at compile time. But we can't say with any certainty which class type pt addresses, so the resolution of the access must be delayed until runtime through an additional indirection.

No comments: