why does return statement assigns value?

May 2, 2016 at 10:51am
why is the return statement in line 13 assigning integer values to array.
All it should do is to return the element right? how is this possible?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// overload_array.cpp
// overloading the c++ array subscript operator []
#include <iostream>

using namespace std;

class myArray {
    private:
      int size;
      int a[10];
    public:
      int& operator[] (int x) {
          return a[x];
      }
      void print_array();   // I included this just to show the operator[] works!   
};

void myArray::print_array()
{
    for (int j=0; j < 10; j++)
        cout << "array[" << j << "] = " << a[j] << "\n";
}

int main(int argc, char *argv[])
{
    // create an instance of the myArray class    
    myArray instance;

    // load instance.a[] with integers
        // NOTE: here we use instance[i] NOT instance.a[i]   
        // instance.a[] wouldn't work as "int a[]" in the myArray class
        // is defined as PRIVATE!!!!!  
    for (int i=0; i < 10; i++)
        instance[i] = i; 

    // show that our operator worked by printing out the array values
    // using the myArray member function myArray::print_array()
    instance.print_array();

    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}
May 2, 2016 at 10:56am
It does not return the value. It returns a reference (&) to the value, so that on line 34 it is possible to assign a value. Remove the & on line 12 and see what happens.
May 2, 2016 at 11:34am
removing (&) operator causes a 'lvalue required' error.
i modified the code to use pointer instead of reference and it works
1
2
3
4
5
int *operator[] (int x) {
     return &a[x];
}

*instance[i] = i+10;


however when i try multidimensional array using reference "declaration of operator[] as non-function" is thrown.

1
2
3
4
5
6
7
private:
      int a[10][10];
    public:
      int& operator[] [] (int x,int y) {
          return a[x][y];
      }


Is it even possible to overload a [] operator for a multi dimesional array?
Last edited on May 2, 2016 at 11:35am
May 2, 2016 at 11:52am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct my_array
{
    int a[10][10] {};

    auto& operator[] ( std::size_t pos ) { return a[pos] ; } // invariant: pos < 10
    const auto& operator[] ( std::size_t pos ) const { return a[pos] ; } // invariant: pos < 10
};

int main()
{
    my_array a ;
    a[3][5] = 99 ;
    std::cout << a[3][5] << '\n' ;
}

http://coliru.stacked-crooked.com/a/cbd6308f3043e946
Last edited on May 2, 2016 at 11:53am
May 2, 2016 at 12:12pm
above code causes an
internal compiler error: in gen_type_die_with_usage, at dwarf2out.c:19483 
in my codeblocks with support for C++11 turned on.

I assume that by my understanding in a two dimensional array the first index stores pointer to starting address of the arrays, the second index access the pos element in the array.

so the first overloaded operator[]
 
auto& operator[] (std::size_t pos){ return a[pos];}

returns a pointer to starting address of 3rd array, and the second operator[]
 
const auto& operator[] { std::size_t pos) const { return a[pos]; }


return the pos element or 5th element of that array.
Is my understanding right? the code is not running on my local machine with g++ 4.8.4 version


Last edited on May 2, 2016 at 12:17pm
May 2, 2016 at 2:20pm
> above code causes an internal compiler error: in gen_type_die_with_usage, at dwarf2out.c:19483
> in my codeblocks with support for C++11 turned on.

Try this version (without C++14 return type deduction):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

struct my_array
{
    int a[10][10] {};
    
    typedef int array_type[10] ;

    array_type& operator[] ( std::size_t pos ) { return a[pos] ; } // invariant: pos < 10
};

int main()
{
    my_array a ;
    a[3][5] = 99 ;
    std::cout << a[3][5] << '\n' ;
}

http://coliru.stacked-crooked.com/a/29197029f3af3cdd

The overloaded operator returns a reference to 'array of 10 int'. ie. a[3] yields a reference to 'array of 10 int'.
The [5] in a[3][5] is the normal subscript operator applied to that array,.
Topic archived. No new replies allowed.