fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Integers
  6. {
  7. public:
  8. Integers(int* values) { _values = values; }
  9. ~Integers() { delete[] _values; };
  10.  
  11. const int& operator[](size_t index) const
  12. {
  13. return _values[index];
  14. }
  15.  
  16. operator const int*() const
  17. {
  18. return _values;
  19. }
  20.  
  21. private:
  22. int& operator[](size_t index)
  23. {
  24. return _values[index];
  25. }
  26.  
  27. operator int*() const
  28. {
  29. return _values;
  30. }
  31.  
  32. int* _values;
  33. };
  34.  
  35. int main()
  36. {
  37. int* values = new int[5] { 0, 1, 2, 3, 4 };
  38. Integers ints(values);
  39. const int& i = ints[2];
  40. printf("int: %d", i);
  41. return 0;
  42. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:39:23: error: ‘int& Integers::operator[](size_t)’ is private within this context
  const int& i = ints[2];
                       ^
prog.cpp:22:7: note: declared private here
  int& operator[](size_t index)
       ^~~~~~~~
stdout
Standard output is empty