Need help with overloading the [] operator

I understand that calling these types of function requires you to cout an array with a specific subscript, but i am having some trouble on actually doing that

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
44
45
46
47
#include <iostream>

using namespace std;

class Plant
{
    int *pointer;
    int num_of_elements;

public:

    Plant (int sub)
    {
        num_of_elements = sub;
        pointer = new int [num_of_elements];

        for (int n = 0; n < num_of_elements; n++)
        {
            pointer [n] = (n * 5);
        }
    }



    int &operator [] (int & sub)              //this function for some reason will not be called
    {
        if (sub < 0 || sub >= num_of_elements)
            cout << "Error, no sub subscript found" << "\n";
        else
            return pointer[sub];
    }
};

int main ()
{

    Plant array(5);

    for (int n = 0; n < 5 ; n++)
    {
        array[n] = (n * 2);
    }

    cout << array[2];             //this is supposed to call the operator function but compiler says there's an error here

}
Last edited on
"Compiler says there's an error"

Why should we have to guess what your compiler says? It doesn't say "there is an error"; it tells some details.

A non-const reference to a const literal value 2 makes undefined behaviour possible, if allowed.
Compiler just says "no match for operator [] in array[2]" and i'm not entirely sure what that means, but I removed the reference variable from the function and it seemed to work.

But there's a few things i still don't understand about overloading the "[]" operator. I mean i know how to call this function but.. am i only allowed to pass a subscript of an array of class objects? Also does line 41 convert the "array" class object to an actual array when i store values in the elements?
Last edited on
Your compiler is not very verbose. That is inconvenient. Some other compiler points out that
the non-const reference parameter cannot take literal 2.


Lets try to illustrate something by adding an another member function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int & Plant::element( int sub )
{
  return pointer[sub];
}

int main ()
{
    Plant foo(5);
    for (int n = 0; n < 5 ; n++)
    {
        foo.element( n ) = (n * 2);
    }
    cout << foo.element( 2 );
    return 0;
}

The element() and operator[] do exactly the same thing. The foo is one object. The foo has member foo.pointer, that points to dynamically allocated (line 15 in your code) array. If you do use invalid subscript, then the dereference operation pointer[sub] would refer to outside of the allocated block. Accessing some memory address does not convert anything into array.


Your code has "big issues" though.

1. What does your op[] return, if the subscript is invalid? Apparently nothing, but the function must return a reference.

2. Your class manages some dynamically allocated memory. You must implement:
- copy constructor
- copy assignment
- destructor
Reasoning: the default implementations for these do not manage that memory correctly.
Topic archived. No new replies allowed.