Pretty lost on defining operator[]

I'm a college student, and a bit lost on part of a homework assignment.

The premise is we have a .h file containing all the function declarations and are commented with what they need to do, and we create a .hpp file that fits the defined criteria to be tested with a .cpp driver.

I'm kind of lost on how to define the operator[] functions, however, and would appreciate any help.

relevant segments of .h
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
template <typename T>
class ArrayList
{ 
private:  
  int m_size;                          // current number of elements
  int m_max;                           // maximum capacity of array m_data
  T* m_data;                           // array to store the elements

  T m_errobj;                          // object to return in case of error

    
public:


  // Purpose: return a read-and-write reference to the element at 
  // the specified position
  // Parameters: i is 0-based index into the ArrayList
  // Returns: the element at position i in the ArrayList
  T& operator[](int i);

  // Purpose: return a read-only reference to the element at 
  // the specified position
  // Parameters: i is 0-based index into the ArrayList
  // Returns: the element at position i in the ArrayList
  const T& operator[](int i) const;



And I'm just lost on what I need to put in the definition.

Thanks.
1
2
3
4
5
6
    // ...
    T& operator[](int i)
    {
        return i<m_size ? m_data[i] : m_errorobj ;
    }
    // ... 
Topic archived. No new replies allowed.