fork download
  1. #include <vector>
  2. using std::size_t;
  3.  
  4. struct Foo {
  5. std::vector<int> bars;
  6. int& operator[](size_t idx) {
  7. return bars[idx];
  8. }
  9. };
  10.  
  11. class Bar {
  12. Foo *foo; //some initialization
  13.  
  14. int& helper(size_t idx) {
  15. return (*foo)[idx];
  16. }
  17.  
  18. void barfoo() {
  19. size_t baz = 1;
  20. int qux;
  21.  
  22. //solution 1
  23. qux = (*foo)[baz]; //works
  24. (*foo)[baz] = 1; //works
  25.  
  26. //solution 2
  27. qux = helper(baz); //works
  28. helper(baz) = 1; //does not work: "expression is not assignable"
  29. }
  30. };
  31.  
  32. int main() {}
  33.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty