fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Example {
  5. public:
  6. int x;
  7. int GetX() const { // GetX is a constant member function
  8. x = x + 1; // This causes an error
  9. return x;
  10. }
  11. };
  12.  
  13. int main() {
  14. Example y;
  15. y.x = 5;
  16.  
  17. std::cout << y.GetX() << std::endl;
  18.  
  19. return 0;
  20. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘int Example::GetX() const’:
prog.cpp:8:14: error: assignment of member ‘Example::x’ in read-only object
      x = x + 1; // This causes an error
              ^
stdout
Standard output is empty