Pointer to Members

It is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtain by applying the operator & to a "fully qualified" class member name. A class member pointer can be declared using the operator ::* with the class name.For example, given the class

class A
{
private:
int m;
public:
void show();
};
We can define a pointer to the member m as follows:
int A::* ip= &A :: m;
The ip pointer created thus acts like class member in that it must be invoked with a class object. The phrase A::* means "pointer to member of a class". The phrase &A::m means the "address of the m member of A class".

Remember, the following statement is not valid:

int *ip = &m;

No comments:

Post a Comment