Thursday, December 2, 2010

Pointers to Members

Given the following:

class A
   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;

The value of pmi is not equal to the value of pi.

pi is an address. pmi is a structure with several components. The number of components depends on whether the class contains virtual functions or not.

No comments:

Post a Comment