Pointers to Members
You may take a class member’s address and assign it to a pointer. An address for a member can be obtained by applying the operator & to a “fully qualified” class member name. A class member pointer can be stated using the::* operator with the class name.
For example,
#include<iostream> using namespace std; class A { public: int m; A( ) { m = 5; } void show() { int a; cout<<"a="<<a<<endl; } }; int main() { A a; int A::*ip = &A::m; //pointer to data member void (A::*pf)(void) = &A::show; //pointer to function cout<<a.*ip<<endl; (a.*pf)( ); // calling show() using pf return 0; }
Output:
We can define a pointer to the member m as follows:
int A::* ip = &A:: m;
In the above statement, A::* means “pointer-to-member of A class”.
The phrase &A::m means the “address of the m member of A Class”