1Pointer to a class member
1.1Concept
Unlike a regular pointer, a pointer to a class member does not point to a specific position. It points to a specific member of a class rather than a specific member of a specific object. Usually, the pointer pointing to the data member is viewed as an offset. Most compilers implement a pointer to a data member into an integer that contains the offset of the member to which the data member is directed, add 1 (add 1 to allow 0 to represent an empty data member pointer ). This offset tells you how many bytes the position of a specific member is from the start point of the object. The syntax for declaring a class member pointer is as follows:
Type classname: * var;
Eg:
Class C
{
Public:
Int _;
};
Int C: * pimc; // a pointer pointing to an int member of C
C AC;
C * Pc = & AC;
Picmc = & C: _;
AC. * pimc = 0;
Int B = pc-> * pimc;
When the value of pimc is set to & C: A _, pimc is actually set to the offset of A _ in C. Unless a _ is a static member, the expression & C: A _ 'does not bring an actual address, but an offset. This offset applies to any object of type C, that is, any object generated by type C can use pimc. When you write down PC-> * pimc, you actually add the address in the PC to the offset in pimc. When AC. * pimc is written, the address in a is added with the offset in pimc.
1.2Features
The pointer to a class member has an implicit conversion from a pointer to a base class member to a pointer to a common class member, however, there is no conversion from a pointer to a derived class member to a pointer to any of its base class members.
In C ++, there is a predefined conversion from a pointer to a derived class to any of its common base classes, and the pointer to a class member is the opposite. For example, the circle class inherits the shape class. A circle * type pointer can be converted to a shape * type pointer.
Class shape
{
Point center;
};
Class circle: Public shape
{
Double Radius;
}
A pointer to a Circle member cannot be converted to a pointer to a shape member because the pointer to a class member is an offset, the parent class, such as shape, may not contain this member in the subclass, such as the radius in the circle. On the contrary, a pointer to a parent class member can be converted to a pointer to a Child class member. Because the Child class inherits the members of the parent class, any offset of the parent class is equally valid in the Child class.
2Pointer to a member function
When obtaining the address of a non-static member function, the obtained address is not an address, but a pointer to the member function.
Class shape
{
Public:
Void moveTo (point location );
Bool validate () const;
};
Class circle: Public shape
{
Bool draw () const;
};
Void (shape: * mf1) (point) = & shape: moveTo;
Bool (shape: * mf2) () = & shape: Validate;
Unlike pointers to common functions, classname is added before the variable name ::. Like a pointer to a data member, an object or a pointer to an object is required to dereference a pointer to a member function.
Circle CIRC;
Shape * pshape = & CIRC;
(Pshape-> * mf2) (); // call shape: Validate
(CIR. * mf2) (); // call shape: Validate
-> * And. * must be added with () because they have lower priority than () operators.
Pointers to member functions are the same as pointers to data members. For example:
Class B
{
Public:
Void bset (INT Val) {bval = val ;}
PRIVATE:
Int bval;
};
Class D: Public B
{
Public:
Void dset (INT Val) {dval = val ;}
PRIVATE:
Int dval;
};
B;
D;
Void (B: * F1) (INT) = & D: dset; // Error
(B. * F1) (12); // error. dval does not exist in B.
Void (D: * F2) (INT) = & B: bset; // OK
(D. * F2) (11); // OK