0

I want to know how to overload the operator [] or = so i can change the values in a linked list like in the vector class, for example.

 LinkedList list;
 list[0] = 2;
Unlucko
  • 1
  • 1
  • Both of those operators are described in the duplicate. – Drew Dormann Sep 20 '21 at 00:45
  • 1
    Note that overloading `operator[]` for a linked list is typically frowned upon, since use of `operator[]` is usually assumed to be sub-linear in cost (typically `O(1)`), while it would be `O(n)` for a linked list. You might take a look at `std::list` to get an idea of the API typically used for a linked list. `operator=` is for reassigning the *whole* list, so again, not appropriate for reassigning individual values. You'd want methods to insert and remove at a given iterator position, and assignable iterators, to allow inserting/removing/changing individual elements. – ShadowRanger Sep 20 '21 at 00:50

0 Answers0