fork download
  1. struct X
  2. {
  3. int data1, data2;
  4. int X::*ptr;
  5.  
  6. X() : ptr(&X::data1) {}
  7. };
  8.  
  9. int main()
  10. {
  11. X a; // now, `a.ptr` points to `a.data1`
  12. X b = a; // `b.ptr` points to `b.data1`
  13.  
  14. a.ptr = &X::data2; // now `a.ptr` points to `a.data2`
  15. // `b.ptr` points to `b.data1`
  16. b = a; // `b.ptr` points to `b.data2` too
  17.  
  18. // Usage hint:
  19. int deref = a.*(a.ptr); // gets the field pointed to by a.ptr, from the instance a
  20. deref = b.*(b.ptr); // gets the field pointed to by b.ptr, from the instance b
  21.  
  22. // but of course you could get fancy and do
  23. deref = a.*(b.ptr); // gets the field pointed to by b.ptr, **but** from the instance a
  24. }
Success #stdin #stdout 0s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty