-1

I'm working on a C++ project and I need to implement subscript operator overloading for a class. Specifically, I want to be able to use the square bracket notation ([]) on an object of my class to access or modify elements in a container.

I have a custom class that represents a collection of data, and I would like to provide a convenient way to access elements within the collection using the subscript operator. Can someone guide me on how to overload the subscript operator in C++?

I would appreciate a detailed explanation of the syntax and implementation, along with any best practices or considerations to keep in mind while implementing this feature.

Thank you in advance for your help!

  • https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/4421719#4421719 – Retired Ninja Jul 14 '23 at 04:01
  • 1
    @RetiredNinja: just a comment on the operator[] advice at that link - "If value_type is known to refer to a built-in type, the const variant of the operator should better return a copy instead of a const reference:" -> I disagree because it's not uncommon for client code to want the addresses of the returned objects, for example to do a diff of pointers to various objects in the container if it's known to be contiguous, or to ask if two functions have identified the same object in the container. – Tony Delroy Jul 14 '23 at 04:05
  • @TonyDelroy There should be two overloads of operator [], one const and one non-const overload. The const one should return a const & (or a copy for cheap types), The non const should return a reference. The references returned are temporary and should not be stored. The reason you give for pointer arithmetic is not tennable because it makes assumptions on the internal (data) structure of the class implementing operator[]. Clients ususally do NOT need pointer artihmetic, for efficient acces just implement a `begin()` and `end()` iterators for that (those may or may not be pointer). – Pepijn Kramer Jul 14 '23 at 04:47
  • So bascially what you do in your example. – Pepijn Kramer Jul 14 '23 at 04:52
  • @PepijnKramer: "The references returned are temporary and should not be stored." - I disagree. People using any container should generally be able to do `auto& v = container[index];` then `update(v);`, `v += k;`, or whatever as a separate statement. "not tennable because it makes assumptions" - it's not *always* tenable, but when you're using a container that documents certain things about the memory addresses used (e.g. `std::vector`), then that can be useful information. – Tony Delroy Jul 14 '23 at 05:30
  • What I meant is the reference should not be stored in a global or member value (or returned from the current function), The reference should just be used within the scope where it as retrieved from the container. So yes your example good use. And yes vector does explicitly has that as a requirement so it is documented which is also fine :) – Pepijn Kramer Jul 14 '23 at 05:34

1 Answers1

3

Basically...

value& operator[](size_t index) { ... }
const value& operator[](size_t index) const { ... }

Just have each return (by reference) the value at that index. The "index" you use can be any single value - e.g. an int, string, knock yourself out....

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252