fork download
  1. int main()
  2. {
  3. /* Values */
  4. int i = 4; // not constant, can be modified:
  5. i = 5;
  6.  
  7. const int ci = 6; // constant, must be initialized
  8. ci = 7; // syntax error, cannot be modified
  9.  
  10. /* References */
  11. int &ir1 = i; // reference, must be initialized
  12. ir1 = 6;
  13.  
  14. int &ir2 = ci; // syntax error, const correctness would be flawed
  15.  
  16. const int &cir1 = ci; // constant reference to a constant value
  17. cir1 = 7; // syntax error
  18.  
  19. const int &cir2 = i; // const reference to a non-const value, still ok
  20. cir2 = 7; // syntax error, cir2 is const reference
  21.  
  22. /* Pointers */
  23. int *ip;
  24. ip = &i;
  25. *ip = 5; // ok
  26.  
  27. ip = &ci; // syntax error, const correctness would be flawed
  28.  
  29. const int *cip = &ci; // ok
  30. *cip = 7; // syntax error
  31.  
  32.  
  33. ip = cip; // syntax error, C++ keeps constness
  34. cip = ip; // ok, but now:
  35. *cip = 5; // syntax error, wherever cip points is const
  36.  
  37. int const *icp; // same as const int *
  38. icp = &i; // ok, can assign to, icp is NOT const, *icp IS
  39. *icp = 5; // syntax error, wherever icp points is const
  40.  
  41. /* Can a pointer be constant? */
  42. int * const ipc = &i; // ipc IS const, must be initialized
  43. *ipc = 5; // OK, where ipc points to is NOT a const
  44.  
  45. int * const ipc2 = &ci; // syntax error, ipc2 is NOT pointer to const
  46. const int * const cipc = &ci; // const pointer to a const
  47.  
  48. return 0;
  49. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:8:19: error: assignment of read-only variable 'ci'
                ci = 7;  // syntax error, cannot be modified
                   ^
prog.cpp:14:21: error: invalid initialization of reference of type 'int&' from expression of type 'const int'
          int &ir2 = ci; // syntax error, const correctness would be flawed
                     ^
prog.cpp:17:19: error: assignment of read-only reference 'cir1'
              cir1 = 7;  // syntax error
                   ^
prog.cpp:20:19: error: assignment of read-only reference 'cir2'
              cir2 = 7;  // syntax error, cir2 is const reference
                   ^
prog.cpp:27:18: error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
               ip = &ci; // syntax error, const correctness would be flawed
                  ^
prog.cpp:30:18: error: assignment of read-only location '* cip'
             *cip = 7;   // syntax error
                  ^
prog.cpp:33:18: error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
               ip = cip; // syntax error, C++ keeps constness
                  ^
prog.cpp:35:18: error: assignment of read-only location '* cip'
             *cip = 5;   // syntax error, wherever cip points is const 
                  ^
prog.cpp:39:18: error: assignment of read-only location '* icp'
             *icp = 5;   // syntax error, wherever icp points is const
                  ^
prog.cpp:45:29: error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
         int * const ipc2 = &ci; // syntax error, ipc2 is NOT pointer to const
                             ^
stdout
Standard output is empty