Overloading the [] Operator

Hey, me again.

Alright, still working on a class assignment (practically re-making the map class out of a vector).

What I'm trying to figure out is how to properly overload the [] operator in order to get this working how I need it.

What I want to be able to do is this:
associativeArrayObject["Elephants"] = 5;

In the AssociativeArray class, I overload the [] operator like so:
void AssociativeArray::operator[](string str) ...

However, that "void" is wrong. I need to return something in order to assign that something to "5". What am I returning? (Optimally the position in the 2D vector where I will be storing the data, but how?)

I have a feeling I'm overlooking something very simple.

Hope that was clear enough, thanks for any help!
Last edited on
You'd return a reference. Assuming the value is an int:

 
int& AssociativeArray::operator[] (string str) ...
Oh wow, I get it now. Wasn't thinking of it that way.

Thanks!
Topic archived. No new replies allowed.