fork download
  1. #include <iostream>
  2.  
  3. class Broken {
  4. public:
  5. Broken(int i): read_only(read_write), read_write(i) {}
  6.  
  7. int const& read_only;
  8.  
  9. void set(int i) { read_write = i; }
  10.  
  11. private:
  12. int read_write;
  13. };
  14.  
  15. int main() {
  16. Broken original(5);
  17. Broken copy(4);
  18. copy = original;
  19.  
  20. std::cout << copy.read_only << "\n";
  21.  
  22. original.set(42);
  23.  
  24. std::cout << copy.read_only << "\n";
  25. return 0;
  26. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:18:7: error: use of deleted function 'Broken& Broken::operator=(const Broken&)'
  copy = original;
       ^
prog.cpp:3:7: note: 'Broken& Broken::operator=(const Broken&)' is implicitly deleted because the default definition would be ill-formed:
 class Broken {
       ^
prog.cpp:3:7: error: non-static reference member 'const int& Broken::read_only', can't use default assignment operator
stdout
Standard output is empty