It is possible to take address of amember of a class and assign it to a pointer. The address of a member can be obtained by applying the operator & to a “fully qualified” class member name. A class member pointer can be declared using the operator ::* with the
For example, given the class
We can define a pointer to the member mas follow:
int A::* ip = &A :: m;
The ip pointer created thus acts like a class member in that it must be invoked with a
class object. In the statement above, the phrase A::* means “pointer-to-member of A class”. The phrase &A::m means the “address of the m memberof A class”.
Remember, the following statement is not valid:
int*ip = &m; //won’t work
This is because m is not simply an int type data. It has meaning only when it is associated with the class to which it
belongs. The scope operator must be applied to both the pointer and the member.
The pointer ip can now be used to access the member m inside member function (or friend functions). Let us assume that a is an object of A declared in a member function. We can access m using the pointer ip as follows: