Thursday, December 2, 2010

Pointers to Members

Given the following:

class A
{
 public:
   int i1;
   int i2; 
   int f1()
   {
      return 1;
   }
   int f2()
   {
      return 2;
   }
};

int main()
{
   int A::*pmi = &A::i1;
   int (A::*pmf)();
   A a;
   A *a2 = new A; 
   a.*pmi = 99;
   int *pi = &a.i1;
   *pi = 98;
   pmf = &A::f1;
   return 0;
}

To use pmi with a2 do a2->*pmi=99;

No comments:

Post a Comment