Pointers to Members

Pointers to members allow you to refer to nonstatic members of class objects. You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object. To point to a static class member , you must use a normal pointer.

You can use pointers to member functions in the same manner as pointers to functions. You can compare pointers to member functions, assign values to them, and use them to call member functions. Note that a member function does not have the same type as a nonmember function that has the same number and type of arguments and the same return type.

To reduce complex syntax , you can declare a typedef to be a pointer to a member. A pointer to a member can be declared and used as shown in the following code fragment :

typedef void (X::*ptfptr) (int);      // declare typedef
void main ()
{
// ...
ptfptr ptf = &X::f;                   // use typedef
X xobject;
(xobject.*ptf) (20);                  // call function
}

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than .* and ->*, you must use parentheses to call the function pointed to by ptf.



Pointer to Member Operators .* ->*
Pointers
Static Members
The this Pointer
Example of Pointers to Members