Thursday 14 May 2015

Pointer to member of class

We can assign the address of a member of a class to a pointer by applying operator & to a "fully qualified" class member name. Pointer to class member can be declared using the operator ::*. For example:

class abc
{
      int x;
      public :
      void show( );
};

Now we can define a pointer to the member x as follows:

int abc::* p=&abc :: x;

The p pointer created thus act like a class member in that it must be invoked with a class object. In the statement above, the phrase abc::* means  "pointer-to-member of abc class". The phrase &abc::x means the "address of the x member of abc class".

abc a;
cout<<a.*p;
cout<<a.x;

Above two statements are identical

No comments:

Post a Comment