fork(1) download
  1. #include <iostream>
  2.  
  3. struct foo {
  4. int x;
  5. int y;
  6. int z;
  7. };
  8.  
  9. struct foo_wrapper {
  10. foo &ref;
  11.  
  12. foo_wrapper( foo &f ) : ref( f ) {}
  13. int &operator[]( std::size_t rhs ) {
  14. switch(rhs) {
  15. case 0U:
  16. return ref.x;
  17. case 1U:
  18. return ref.y;
  19. case 2U:
  20. return ref.z;
  21. default:
  22. return *(&(ref.z) + rhs - 2U);
  23. }
  24. }
  25. };
  26.  
  27. foo_wrapper wrap( foo &ff )
  28. {
  29. return foo_wrapper( ff );
  30. }
  31.  
  32. int main() {
  33. foo f;
  34. wrap( f )[1] = 123;
  35. std::cout << f.y << std::endl;
  36. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
123