fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct foo {
  5. int x;
  6. int y;
  7. int z;
  8. };
  9.  
  10. int& operator [](foo& lhs, const std::size_t rhs) {
  11. switch(rhs) {
  12. case 0U:
  13. return lhs.x;
  14. case 1U:
  15. return lhs.y;
  16. case 2U:
  17. return lhs.z;
  18. default:
  19. return *(&(lhs.z) + rhs - 2U);
  20. }
  21. }
  22.  
  23. int main() {
  24. foo my_foo{};
  25.  
  26. cout << foo[0U] << endl;
  27. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:10:49: error: ‘int& operator[](foo&, std::size_t)’ must be a nonstatic member function
 int& operator [](foo& lhs, const std::size_t rhs) {
                                                 ^
prog.cpp: In function ‘int main()’:
prog.cpp:26:13: error: expected primary-expression before ‘[’ token
  cout << foo[0U] << endl;
             ^
stdout
Standard output is empty