C/C++ Arena

Step 4 of 5

operator[]

operator[] must be a member function. Returning a reference lets callers assign through it. Usually you provide a const and a non-const version:

int& operator[](int i) { return data_[i]; }
const int& operator[](int i) const { return data_[i]; }

Your turn: give Loadout an operator[] taking a slot number 1 to 5 (not 0 to 4!) and returning the weapon name in that slot.

Previous: Printing with operator<< Next: operator< and sorting