fork download
  1. #include <vector>
  2.  
  3. class Array {
  4. std::vector<int> Arr;
  5. int n;
  6. public:
  7. Array(int _n = 0) : Arr(_n), n(_n) {
  8. }
  9. ~Array(void) {
  10. }
  11. friend void f(const Array &A) {
  12. A.Arr[0] = 3; // why can the Arr[0], Arr[1] be changed the value ?
  13. A.Arr[1] = 4;
  14. // A.n = 10; // can not be changed because of 'const class type'
  15. }
  16. };
  17.  
  18. void main()
  19. {
  20. Array A(5);
  21. f(A);
  22. }
  23.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void f(const Array&)':
prog.cpp:12:18: error: assignment of read-only location 'A.Array::Arr.std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(0u)'
         A.Arr[0] = 3;  // why can the Arr[0], Arr[1] be changed the value ?
                  ^
prog.cpp:13:18: error: assignment of read-only location 'A.Array::Arr.std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(1u)'
         A.Arr[1] = 4;
                  ^
prog.cpp: At global scope:
prog.cpp:18:11: error: '::main' must return 'int'
 void main()
           ^
stdout
Standard output is empty