[try Beta version]
Not logged in

 
How to overload the [] operator twice?

Jul 10, 2014 at 6:10pm
Hello guys,

Here's my question. I'm coding a basic Linked List class (for the purpose of understanding and having fun, I know about STL), LList.

I have overloaded the [] operator so it returns the data of the index-th node in the list, for example, if I code

LList x;
....
cout<<x[5];

it prints the data of the 5th node in the list (for fun I decided to index from 1 to infinity).

My question: Now I want to be able to assign the value to the index-th node data, using [] and =, for example, I want to be able to write:

LList x;
.....
x[5] = 121;

How can I do that? I've been googling but only find the info about how to
Jul 10, 2014 at 6:17pm
iirc it should let you work with the [] operator you already have
Jul 10, 2014 at 6:17pm
Let your operator[] return a reference (LList&) to the object. That's how the "STL" does it.
Jul 10, 2014 at 6:22pm
Wow, that's really cool! Thank you, Peter87!

It didn't, because I didn't return the address, Little Bobby Tables!
Topic archived. No new replies allowed.